Return ``num_bits`` bits, taking into account endianness and 
        left-right bit directions
499283
def read_bits(self, stream, num_bits, padded, left_right, endian):
        
        if self._cls_bits is None and padded:
            raw_bits = stream.read_bits(self.cls.width*8)
            self._cls_bits = self._endian_transform(raw_bits, endian)

        if self._cls_bits is not None:
            if num_bits > len(self._cls_bits):
                raise errors.PfpError("BitfieldRW reached invalid state")

            if left_right:
                res = self._cls_bits[:num_bits]
                self._cls_bits = self._cls_bits[num_bits:]
            else:
                res = self._cls_bits[-num_bits:]
                self._cls_bits = self._cls_bits[:-num_bits]

            return res

        else:
            return stream.read_bits(num_bits)
720427
def gray(num_bits):
    
    last = 0
    x = 0
    n = 1 << num_bits
    while n > x:
        bit_string = bin(n + x ^ x // 2)[3:]
        value = int(bit_string, 2)
        yield bit_string, value, changed_bit_pos(last, value)
        last = value
        x += 1
906015
def l2n(l, c):
    ""
    c = []
    c.append(int((l >> 24) & U32(0xFF)))
    c.append(int((l >> 16) & U32(0xFF)))
    c.append(int((l >>  8) & U32(0xFF)))
    c.append(int((l      ) & U32(0xFF)))
    return c
840602
def target_from_bits(bits)
      exponent = ((bits >> 24) & 0xff)
      mantissa = bits & 0x7fffff
      mantissa *= -1 if (bits & 0x800000) > 0
      (mantissa * (256**(exponent-3))).to_i
    end
511169
def all_bitstrings(n_bits):
    
    n_bitstrings = 2 ** n_bits
    out = np.zeros(shape=(n_bitstrings, n_bits), dtype=np.int8)

    tf = np.array([False, True])
    for i in range(n_bits):
        # Lexicographical ordering gives a pattern of 1's
        # where runs of 1s of length 2**j are tiled 2**i times

        # i indexes from the *left*
        # j indexes from the *right*
        j = n_bits - i - 1

        out[np.tile(np.repeat(tf, 2 ** j), 2 ** i), i] = 1
    return out
9988
function reverseBits(x, bits) {
      var y = 0;

      for (var i = 0; i < bits; i++) {
        y = (y << 1) | (x & 1);
        x >>>= 1;
      }

      return y;
    }
809430
def byte_to_bitstring(byte):
    """"""
    assert 0 <= byte <= 0xff
    bits = [int(x) for x in list(bin(byte + 0x100)[3:])]
    return bits
1160435
def read_uint16(self, little_endian=True):
        
        if little_endian:
            endian = "<"
        else:
            endian = ">"
        return self.unpack('%sH' % endian, 2)
229355
function _convertNumber($num)
    {
        // Integer in the range 0..2**16-1
        if ((preg_match("/^\d+$/", $num)) and ($num <= 65535)) {
            return pack("Cv", $this->ptg['ptgInt'], $num);
        } else { // A float
            if ($this->_byte_order) { // if it's Big Endian
                $num = strrev($num);
            }
            return pack("Cd", $this->ptg['ptgNum'], $num);
        }
    }
241647
function(left, right) {
  if (!left && !right) {
    return { high: Long.fromNumber(0), low: Long.fromNumber(0) };
  }

  var leftHigh = left.shiftRightUnsigned(32);
  var leftLow = new Long(left.getLowBits(), 0);
  var rightHigh = right.shiftRightUnsigned(32);
  var rightLow = new Long(right.getLowBits(), 0);

  var productHigh = leftHigh.multiply(rightHigh);
  var productMid = leftHigh.multiply(rightLow);
  var productMid2 = leftLow.multiply(rightHigh);
  var productLow = leftLow.multiply(rightLow);

  productHigh = productHigh.add(productMid.shiftRightUnsigned(32));
  productMid = new Long(productMid.getLowBits(), 0)
    .add(productMid2)
    .add(productLow.shiftRightUnsigned(32));

  productHigh = productHigh.add(productMid.shiftRightUnsigned(32));
  productLow = productMid.shiftLeft(32).add(new Long(productLow.getLowBits(), 0));

  // Return the 128 bit result
  return { high: productHigh, low: productLow };
}
867875
def _make_short(self, data, endianness):
        """"""
        data = binwalk.core.compat.str2bytes(data)
        return struct.unpack('%sH' % endianness, data)[0]
689855
def each_bit
      return enum_for(:each_bit) unless block_given?
      index = 0
      headbits = (self.bytesize - 1) * 8
      skipbits = 8 - @bits
      @data.each_byte do |byte|
        loop do
          break if index == @bitsize
          if index >= headbits
            bit = (byte >> (7 - ((index + skipbits) & 7))) & 1
          else
            bit = (byte >> (7 - (index & 7))) & 1
          end
          yield bit
          index += 1
          break if (index & 7) == 0
        end
      end
      return self
    end
970184
def bits_in(length, keyspace):
    
    keyspace = len(keyspace)
    length_per_cycle = 64
    if length > length_per_cycle:
        bits = 0
        length_processed = 0
        cycles = ceil(length / length_per_cycle)
        for _ in range(int(cycles)):
            if length_processed + length_per_cycle > length:
                length_per_cycle = length - length_processed
            bits += calc_bits_in(length_per_cycle, keyspace)
            length_processed += length_per_cycle
    else:
        bits = calc_bits_in(length, keyspace)
    return float(abs(bits))
877436
def print_num(num):
    '''  '''
    out('hex: 0x{0:08x}'.format(num))
    out('dec:   {0:d}'.format(num))
    out('oct: 0o{0:011o}'.format(num))
    out('bin: 0b{0:032b}'.format(num))
1158678
def parseBits(self, hexcode, width):
        """"""
        bitarray = []
        for byte in hexcode[::-1]:
            bits = int(byte, 16)
            for x in range(4):
                bitarray.append(bool((2 ** x) & bits))
        bitarray = bitarray[::-1]
        return enumerate(bitarray[:width])
105403
def _get_unsigned_params(conv):
    
    conv["sign_mask"] = 0
    conv["int_min"] = 0
    conv["int_mask"] = sum([2 ** i for i in range(conv["bin_point"], 
        conv["bits"])])
    conv["int_max"] = sum([2 ** i for i in range(conv["bits"] -
        conv["bin_point"])])
795367
def read_int64(self, little_endian=True):
        
        if little_endian:
            endian = "<"
        else:
            endian = ">"
        return self.unpack('%sq' % endian, 8)