public final int hgetbits(int N) {
        totbit += N;
        val=0;
        if (buf_byte_idx + N < BUFSIZE) {
            while (N-- > 0) {
                val <<= 1;
                val |= ((buf[buf_byte_idx++] == 0) ? 0 : 1);
            }
        } else {
            while (N-- > 0) {
                val <<= 1;
                val |= ((buf[buf_byte_idx] == 0) ? 0 : 1);
                buf_byte_idx = (buf_byte_idx + 1) & BUFSIZE_MASK;
            }
        }
        
        return val;
        
    }