public void putBit(int bit) throws IOException {
        if (!(bit == 0 || bit == 1)) {
            throw new IllegalArgumentException("argument must be 0 or 1");
        }

        // If the buffer has eight bits, need to flush it first.
        if (numBits == 8) {
            flushBits();
        }

        // Add the new bit to the right of the existing buffer.
        buffer = (byte)((buffer << 1) | bit);
        
        numBits++;
    }