returns how many bits long the bigInt is, not counting leading zeros.
553776
function bitSize(x) {
  var j, z, w;
  for (j=x.length-1; (x[j]==0) && (j>0); j--);
  for (z=0, w=x[j]; w; (w>>=1), z++);
  z+=bpe*j;
  return z;
}
1304940
def bitceil(N):
    
    # Note: for Python 2.7 and 3.x, this is faster:
    # return 1 << int(N - 1).bit_length()
    N = int(N) - 1
    for i in [1, 2, 4, 8, 16, 32]:
        N |= N >> i
    return N + 1
549872
def decode_int(v):
    """"""
    if len(v) > 0 and (v[0] == b'\x00' or v[0] == 0):
        raise Exception("No leading zero bytes allowed for integers")
    return big_endian_to_int(v)
1309525
public long getUnsignedIntAt(int index) {
        _checkForReadAt(index,4);
        long val = (long) (_buf.getInt(index) & 0xffffffffL);
        return val;
    }
652967
private static long longSqrt(long value)
	{
		final int scale = 8;
		int bits = 64;
		long sqrt=0;
		long rest=0;
		for (int i=0; i<scale; i++)
		{
			bits -= 8;
			rest = (rest<<8) | ((value>>bits)&0xFF);
			long i2 = (sqrt<<5)+1;
			
			int k0=0;
			while (true)
			{
				long i3 = rest-i2;
				if (i3<0) break;
				
				rest=i3;
				i2+=2;
				k0++;
			}
			sqrt=(sqrt<<4)+k0;
		}
		
		return sqrt;
	}
950732
def _count_righthand_zero_bits(number, bits):
    
    if number == 0:
        return bits
    return min(bits, _compat_bit_length(~number & (number - 1)))
1293685
def length(self):
        
        if isinstance(self._value, int):
            return byte_len(self._value)
        if self._value is None:
            return 0
        return len(self._value)
949798
def pack_bits( longbits ):
    """"""
    byte = longbits & (0x0101010101010101)
    byte = (byte | (byte>>7)) & (0x0003000300030003)
    byte = (byte | (byte>>14)) & (0x0000000f0000000f)
    byte = (byte | (byte>>28)) & (0x00000000000000ff)
    return byte
368506
def size(self):
        '''
            
        '''
        t = self._type
        if t.startswith('uint'):
            return int(t[len('uint'):])
        if t.startswith('int'):
            return int(t[len('int'):])
        if t == 'bool':
            return int(8)
        if t == 'address':
            return int(160)
        if t.startswith('bytes'):
            return int(t[len('bytes'):])
        return None
926510
def size_of_varint_2(value):
    
    value = (value << 1) ^ (value >> 63)
    if value <= 0x7f:
        return 1
    if value <= 0x3fff:
        return 2
    if value <= 0x1fffff:
        return 3
    if value <= 0xfffffff:
        return 4
    if value <= 0x7ffffffff:
        return 5
    if value <= 0x3ffffffffff:
        return 6
    if value <= 0x1ffffffffffff:
        return 7
    if value <= 0xffffffffffffff:
        return 8
    if value <= 0x7fffffffffffffff:
        return 9
    return 10
303328
def wr_long(f, x):
    """"""
    if PYTHON3:
        f.write(bytes([x        & 0xff]))
        f.write(bytes([(x >> 8)  & 0xff]))
        f.write(bytes([(x >> 16) & 0xff]))
        f.write(bytes([(x >> 24) & 0xff]))
    else:
        f.write(chr( x        & 0xff))
        f.write(chr((x >> 8)  & 0xff))
        f.write(chr((x >> 16) & 0xff))
        f.write(chr((x >> 24) & 0xff))
71135
public int count()
    {
        int count = 0;
        for (int ii=bits-1;ii>=0;ii--)
        {
            if (isSet(ii))
            {
                count++;
            }
        }
        return count;
    }
436867
def _number_of_set_bits(x):
    '''
    
    '''
    # Taken from http://stackoverflow.com/a/4912729. Many thanks!
    x -= (x >> 1) & 0x55555555
    x = ((x >> 2) & 0x33333333) + (x & 0x33333333)
    x = ((x >> 4) + x) & 0x0f0f0f0f
    x += x >> 8
    x += x >> 16
    return x & 0x0000003f
1135952
public static int cardinality(long[] bits) {
    int card = 0;
    for (int i = bits.length - 1; i >= 0; i--) {
      long a = bits[i];
      if (a == 0)
        continue;
      if (a == -1) {
        card += 64;
        continue;
      }
      // Successively collapse alternating bit groups into a sum.
      a = ((a >> 1) & 0x5555555555555555L) + (a & 0x5555555555555555L);
      a = ((a >> 2) & 0x3333333333333333L) + (a & 0x3333333333333333L);
      int b = (int) ((a >>> 32) + a);
      b = ((b >> 4) & 0x0f0f0f0f) + (b & 0x0f0f0f0f);
      b = ((b >> 8) & 0x00ff00ff) + (b & 0x00ff00ff);
      card += ((b >> 16) & 0x0000ffff) + (b & 0x0000ffff);
    }
    return card;
  }
627429
def _long2bytes(n, blocksize=0):
    

    # After much testing, this algorithm was deemed to be the fastest.
    s = ''
    pack = struct.pack
    while n > 0:
        ### CHANGED FROM '>I' TO '<I'. (DCG)
        s = pack('<I', n & 0xffffffffL) + s
        ### --------------------------
        n = n >> 32

    # Strip off leading zeros.
    for i in range(len(s)):
        if s[i] <> '\000':
            break
    else:
        # Only happens when n == 0.
        s = '\000'
        i = 0

    s = s[i:]

    # Add back some pad bytes. This could be done more efficiently
    # w.r.t. the de-padding being done above, but sigh...
    if blocksize > 0 and len(s) % blocksize:
        s = (blocksize - len(s) % blocksize) * '\000' + s

    return s
383095
public static int longToIntWithBitMixing(long in) {
		in = (in ^ (in >>> 30)) * 0xbf58476d1ce4e5b9L;
		in = (in ^ (in >>> 27)) * 0x94d049bb133111ebL;
		in = in ^ (in >>> 31);
		return (int) in;
	}
1032782
def get_bits_needed(n):
    

    assert n > 0, "Inout should be positive"

    num_bits = int(np.ceil(np.log2(n)))
    return max(1, num_bits)