bitCount

bitCount(integer x) → integer bits

Returns the number of bits set to 1 in the binary representation of the integer x (its Hamming weight). The count covers the 64 bits of a signed integer: a negative number has its sign bit at 1, and bitCount(-1) returns 64 since all its bits are at 1. The function also works on big integers, without the 64-bit limit; for a negative big integer, the bits at 0 are counted instead (bitCount(-1L) returns 0).

bitCount(0)    // 0
bitCount(7)    // 3: 111 in binary
bitCount(8)    // 1: 1000 in binary
bitCount(255)  // 8: 11111111 in binary
bitCount(-1)   // 64: all bits are at 1
Math.bitCount(0)    // 0
Math.bitCount(7)    // 3: 111 in binary
Math.bitCount(8)    // 1: 1000 in binary
Math.bitCount(255)  // 8: 11111111 in binary
(0).bit_count()    # 0
(7).bit_count()    # 3: 111 in binary
(8).bit_count()    # 1: 1000 in binary
(255).bit_count()  # 8: 11111111 in binary
Parameters
Return
Note