private int bits(int need) throws IOException
   {
      int val; /* bit accumulator */

      /* load at least need bits into val */
      val = m_bitbuf;
      while (m_bitcnt < need)
      {
         if (m_left == 0)
         {
            m_in = m_input.read();
            m_left = m_in == -1 ? 0 : 1;
            if (m_left == 0)
            {
               throw new IOException("out of input"); /* out of input */
            }
         }
         val |= m_in << m_bitcnt; /* load eight bits */
         m_left--;
         m_bitcnt += 8;
      }

      /* drop need bits and update buffer, always zero to seven bits left */
      m_bitbuf = val >> need;
      m_bitcnt -= need;

      /* return need bits, zeroing the bits above that */
      return val & ((1 << need) - 1);
   }
private int bits ( int need ) throws IOException { int val ; val = m_bitbuf ; while ( m_bitcnt < need ) { if ( m_left == 0 ) { m_in = m_input . read ( ) ; m_left = m_in == - 1 ? 0 : 1 ; if ( m_left == 0 ) { throw new IOException ( "out of input" ) ; } } val |= m_in << m_bitcnt ; m_left -- ; m_bitcnt += 8 ; } m_bitbuf = val > > need ; m_bitcnt -= need ; return val & ( ( 1 << need ) - 1 ) ; }
Return need bits from the input stream.  This always leaves less than
eight bits in the buffer.  bits() works properly for need == 0.

Format notes:

- Bits are stored in bytes from the least significant bit to the most
significant bit.  Therefore bits are dropped from the bottom of the bit
buffer, using shift right, and new bytes are appended to the top of the
bit buffer, using shift left.

@param need number of bits required
@return bit values