public void writeCode(Code code) throws IOException {
        // call putBit() on all bits, from left to right
        for (int i = code.length() - 1; i >= 0; i--) {
            putBit(code.getBit(i));
        }
    }