# NumPy Binary operations: packbits() function

## numpy.packbits() function

The packbits() function is used to pack the elements of a binary-valued array into bits in a uint8 array. The result is padded to full bytes by inserting zero bits at the end.

Version: 1.15.0

Syntax:

`numpy.packbits(myarray, axis=None)`

Parameter:

Name Description Required /
Optional
myarray An array of integers or booleans whose elements should be packed to bits. Required
axis The dimension over which bit-packing is done. None implies packing the flattened array. Optional

Return value:

packed [ndarray]
Array 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 axis is None, in which case the output is 1-D).

Example-1: numpy.packbits() function

``````>>> import numpy as np
>>> x = np.array([[[1,1,0], [0,1,0]], [[1,0,1], [0,0,1]]])
>>> y = np.packbits(x, axis=-1)
>>> y
array([[[192],
[ 64]],

[[160],
[ 32]]], dtype=uint8)
``````

Python Code Editor:

Previous: right_shift()
Next: unpackbits()

﻿