bitLength

bitLength(integer x) → integer length

Returns the number of significant bits needed to represent the number x*, that is, the position of the highest bit at 1, plus one. Returns 0 when *x is 0.

For a positive number, the result is equal to floor(log2(x)) + 1. The function also works on big integers.

A negative integer returns 64, since its sign bit is at 1. For a negative big integer, the sign bit is not counted: bitLength(-1L) returns 0 and bitLength(-8L) returns 3.

bitLength(0)    // 0: no bits
bitLength(1)    // 1: 1 in binary
bitLength(8)    // 4: 1000 in binary
bitLength(255)  // 8: 11111111 in binary
Math.bitLength(0)   // 0: no bits
Math.bitLength(1)   // 1: 1 in binary
Math.bitLength(8)   // 4: 1000 in binary
Math.bitLength(255) // 8: 11111111 in binary
(0).bit_length()   # 0: no bits
(1).bit_length()   # 1: 1 in binary
(8).bit_length()   # 4: 1000 in binary
(255).bit_length() # 8: 11111111 in binary
Parameters
Return
Note