bundles / numpy 2.5.0.dev0+git20251130.2de293a / numpy / _core / _multiarray_umath / packbits
built-in
numpy._core._multiarray_umath:packbits
Signature
packbits ( a , / , axis = None , bitorder = big ) Summary
Packs the elements of a binary-valued array into bits in a uint8 array.
Extended Summary
The result is padded to full bytes by inserting zero bits at the end.
Parameters
a: array_likeAn array of integers or booleans whose elements should be packed to bits.
axis: int, optionalThe dimension over which bit-packing is done.
Noneimplies packing the flattened array.bitorder: {'big', 'little'}, optionalThe order of the input bits. 'big' will mimic bin(val),
[0, 0, 0, 0, 0, 0, 1, 1] => 3 = 0b00000011, 'little' will reverse the order so[1, 1, 0, 0, 0, 0, 0, 0] => 3. Defaults to 'big'.
Returns
packed: ndarrayArray of type uint8 whose elements represent bits corresponding to the logical (0 or nonzero) value of the input elements. The shape of packed has the same number of dimensions as the input (unless
axisis None, in which case the output is 1-D).
Examples
import numpy as np a = np.array([[[1,0,1], [0,1,0]], [[1,1,0], [0,0,1]]]) b = np.packbits(a, axis=-1) bNote that in binary 160 = 1010 0000, 64 = 0100 0000, 192 = 1100 0000, and 32 = 0010 0000.
See also
- unpackbits
Unpacks elements of a uint8 array into a binary-valued output array.
Aliases
-
numpy._core._multiarray_umath.packbits