warp.bit_count#

warp.bit_count(x: Int) → int32#
  • Kernel: true
  • Python: true
  • Differentiable: false

Return the number of set bits in an integer.

Signed inputs use their two’s-complement representation, including the sign bit. In contrast, Python’s int.bit_count() counts set bits in the absolute value, so the results can differ for negative inputs.

Parameters:

x – A signed or unsigned scalar integer of 8, 16, 32, or 64 bits. Python int inputs are treated as int32.

Returns:

The number of set bits in the fixed-width binary representation of x.

Example

>>> wp.bit_count(45)
4
>>> wp.bit_count(wp.int8(-1))
8
>>> wp.bit_count(wp.int64(-1))
64
>>> (-1).bit_count()  # Python counts bits in the absolute value.
1