public int getBit() throws IOException {
        // If the buffer is empty, read more bits from the file.
        if (numBits == 0) {
            int returnVal = in.read();
            if (returnVal == -1) {
                return -1;                    // eof has been reached 
            } else {
                buffer = (byte)returnVal;
                numBits = 8;                  // we just read 8 bits
            }
        }

        // Now we need to get the highest bit that is valid:
        // i.e., the bit in position numBits - 1 in the buffer.
        int bit = (buffer & (1 << (numBits - 1))) >> (numBits - 1);
        numBits--;

        return bit;
    }