- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Unpack elements of a uint8 array and trim off that many bits from the end in Numpy

To unpack elements of a uint8 array into a binary-valued output array, use the **numpy.unpackbits()** method in Python Numpy. The result is binary-valued (0 or 1). The axis is the dimension over which bit-unpacking is done. The axis is set using the "axis" parameter. To set the number of elements to unpack, use the "count" parameter.

Each element of the input array represents a bit-field that should be unpacked into a binary-valued output array. The shape of the output array is either 1-D (if axis is None) or the same shape as the input array with unpacking done along the axis specified.

The axis is the dimension over which bit-unpacking is done. None implies unpacking the flattened array. The count parameter is the number of elements to unpack along axis, provided as a way of undoing the effect of packing a size that is not a multiple of eight. A non-negative number means to only unpack count bits. A negative number means to trim off that many bits from the end. None means to unpack the entire array (the default). Counts larger than the available number of bits will add zero padding to the output. Negative counts must not exceed the available number of bits.

The order is the order of the returned bits. 'big' will mimic bin(val), 3 = 0b00000011 ⇒ [0, 0, 0, 0, 0, 0, 1, 1], 'little' will reverse the order to [1, 1, 0, 0, 0, 0, 0, 0]. Defaults to 'big'.

## Steps

At first, import the required library −

import numpy as np

Create a 2d array. We have set uint8 type using the "dtype" parameter −

arr = np.array([[4, 8], [6, 19], [27, 35]], dtype=np.uint8)

Displaying our array −

print("Array...

",arr)

Get the datatype −

print("

Array datatype...

",arr.dtype)

Get the dimensions of the Array −

print("

Array Dimensions...

",arr.ndim)

Get the shape of the Array −

print("

Our Array Shape...

",arr.shape)

Get the number of elements of the Array −

print("

Elements in the Array...

",arr.size)

To unpack elements of a uint8 array into a binary-valued output array, use the numpy.unpackbits() method. To set the number of element to unpack, use the "count" parameter. A negative number means to trim off that many bits from the end. We have set -4 for count, that means we will trim off 4 bits from the end −

res = np.unpackbits(arr, axis = 1, count = -4) print("

Result...

",res)

## Example

import numpy as np # Create a 2d array # We have set uint8 type using the "dtype" parameter arr = np.array([[4, 8], [6, 19], [27, 35]], dtype=np.uint8) # Displaying our array print("Array...

",arr) # Get the datatype print("

Array datatype...

",arr.dtype) # Get the dimensions of the Array print("

Array Dimensions...

",arr.ndim) # Get the shape of the Array print("

Our Array Shape...

",arr.shape) # Get the number of elements of the Array print("

Elements in the Array...

",arr.size) # To unpack elements of a uint8 array into a binary-valued output array, use the numpy.unpackbits() method in Python Numpy # The result is binary-valued (0 or 1). # The axis is the dimension over which bit-unpacking is done. # The axis is set using the "axis" parameter # To set the number of element to unpack, use the "count" parameter # A negative number means to trim off that many bits from the end # We have set -4 for count, that means we will trim off 4 bits from the end res = np.unpackbits(arr, axis = 1, count = -4) print("

Result...

",res)

## Output

Array... [[ 4 8] [ 6 19] [27 35]] Array datatype... uint8 Array Dimensions... 2 Our Array Shape... (3, 2) Elements in the Array... 6 Result... [[0 0 0 0 0 1 0 0 0 0 0 0] [0 0 0 0 0 1 1 0 0 0 0 1] [0 0 0 1 1 0 1 1 0 0 1 0]]

- Related Articles
- Unpack elements of a uint8 array and only unpack some bits in Numpy
- Unpack elements of a uint8 array into a binary-valued output array in Numpy
- Pack the elements of a binary-valued Numpy array into bits in a uint8 array
- Unpack elements of a uint8 array into a binary-valued output array over specific axis in Numpy
- Unpack elements of a uint8 array into a binary-valued output array over axis 0 in Numpy
- Unpack elements and set the unpack count larger than the available number of bits in Numpy
- Pack the elements of a binary-valued Numpy array into bits in a uint8 array over axis 1
- Pack the elements of a binary-valued Numpy array into bits in a uint8 array over negative axis
- Pack the elements of a binary-valued array into bits in a uint8 array over specific axis in Numpy
- Shift the bits of array elements of a Two-Dimensional array to the right in Numpy
- Shift the bits of array elements of a Two-Dimensional array to the left in Numpy
- Shift the bits of integer array elements to the left in Numpy
- Shift the bits of integer array elements to the right in Numpy
- Trim off '?' from strings in JavaScript
- Append values to the end of a masked array in Numpy