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
numpy.bitwise_count(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x | array_like, unsigned int | Input array containing unsigned integers whose bitwise 1-counts will be computed. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where the result is stored. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying which elements to compute. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when computing the bitwise count. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines 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.
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.
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.
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.
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.