NumPy bitwise_count()

The numpy.bitwise_count() function computes the number of 1-bits in the absolute value of each element in an input array. This function is analogous to int.bit_count() in Python or popcount in C++.

Syntax

</>
Copy
numpy.bitwise_count(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
xarray_like, unsigned intInput array containing unsigned integers whose bitwise 1-counts will be computed.
outndarray, None, or tuple of ndarray and None, optionalOptional output array where the result is stored. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying which elements to compute. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior when computing the bitwise count.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output array.
subokbool, optionalDetermines if subclasses of ndarray are preserved in the output.

Return Value

Returns an array with the number of 1-bits in each element of the input array. The output data type is always uint8, and if the input is a scalar, a scalar is returned.


Examples

1. Counting 1-Bits in a Single Integer

Here, we compute the number of 1-bits in the binary representation of a single unsigned integer.

</>
Copy
import numpy as np

# Define a single unsigned integer
num = np.uint32(29)  # Binary: 11101

# Compute the number of 1-bits
result = np.bitwise_count(num)

# Print the result
print("Number of 1-bits in 29:", result)

Output:

Number of 1-bits in 29: 4

2. Counting 1-Bits in an Array

We compute the number of 1-bits for each element in an array of unsigned integers.

</>
Copy
import numpy as np

# Define an array of unsigned integers
numbers = np.array([3, 7, 15, 31], dtype=np.uint32)  # Binary: [11, 111, 1111, 11111]

# Compute the number of 1-bits for each element
bit_counts = np.bitwise_count(numbers)

# Print the results
print("Input numbers:", numbers)
print("Number of 1-bits:", bit_counts)

Output:

Input numbers: [ 3  7 15 31]
Number of 1-bits: [2 3 4 5]

3. Using the out Parameter

Using an output array to store results instead of creating a new array.

</>
Copy
import numpy as np

# Define an array of unsigned integers
numbers = np.array([255, 1023, 4095], dtype=np.uint32)  # Binary: [11111111, 1111111111, 111111111111]

# Create an output array with the same shape
output_array = np.empty_like(numbers, dtype=np.uint8)

# Compute bitwise count and store the result in output_array
np.bitwise_count(numbers, out=output_array)

# Print the results
print("Computed bitwise counts:", output_array)

Output:

Computed bitwise counts: [ 8 10 12]

4. Using the where Parameter

Using a condition to compute the bit count only for selected elements.

</>
Copy
import numpy as np

# Define an array of unsigned integers
numbers = np.array([15, 31, 63, 127], dtype=np.uint32)  # Binary: [1111, 11111, 111111, 1111111]

# Define a mask (compute bitwise count only where mask is True)
mask = np.array([True, False, True, False])

# Compute bitwise count where mask is True
result = np.bitwise_count(numbers, where=mask)

# Print the results
print("Computed bitwise counts with mask:", result)

Output:

Computed bitwise counts with mask: [4 0 6 0]

The bitwise counts are computed only for elements where mask=True. The other values remain unchanged.