Numpy | Binary Operations

Binary operators acts on bits and performs bit by bit operation. Binary operation is simply a rule for combining two values to create a new value.

numpy.bitwise_and() : This function is used to Compute the bit-wise AND of two array element-wise. This function computes the bit-wise AND of the underlying binary representation of the integers in the input arrays.

Code #1 :


# Python program explaining
# bitwise_and() function

import numpy as geek
in_num1 = 10
in_num2 = 11

print ("Input  number1 : ", in_num1)
print ("Input  number2 : ", in_num2) 
  
out_num = geek.bitwise_and(in_num1, in_num2) 
print ("bitwise_and of 10 and 11 : ", out_num) 

Output :

Input  number1 :  10
Input  number2 :  11
bitwise_and of 10 and 11 :  10

 
Code #2 :

# Python program explaining
# bitwise_and() function

import numpy as geek

in_arr1 = [2, 8, 125]
in_arr2 = [3, 3, 115]
 
print ("Input array1 : ", in_arr1) 
print ("Input array2 : ", in_arr2)
  
out_arr = geek.bitwise_and(in_arr1, in_arr2) 
print ("Output array after bitwise_and: ", out_arr) 

Output :

Input array1 :  [2, 8, 125]
Input array2 :  [3, 3, 115]
Output array after bitwise_and:  [  2   0 113]

 
numpy.bitwise_or() : This function is used to Compute the bit-wise OR of two array element-wise. This function computes the bit-wise OR of the underlying binary representation of the integers in the input arrays.

Code #1 :


# Python program explaining
# bitwise_or() function

import numpy as geek
in_num1 = 10
in_num2 = 11

print ("Input  number1 : ", in_num1)
print ("Input  number2 : ", in_num2) 
  
out_num = geek.bitwise_or(in_num1, in_num2) 
print ("bitwise_or of 10 and 11 : ", out_num) 

Output :

Input  number1 :  10
Input  number2 :  11
bitwise_or of 10 and 11 :  11

 
Code #2 :


# Python program explaining
# bitwise_or() function

import numpy as geek

in_arr1 = [2, 8, 125]
in_arr2 = [3, 3, 115]
 
print ("Input array1 : ", in_arr1) 
print ("Input array2 : ", in_arr2)
  
out_arr = geek.bitwise_or(in_arr1, in_arr2) 
print ("Output array after bitwise_or: ", out_arr) 

Output :

Input array1 :  [2, 8, 125]
Input array2 :  [3, 3, 115]
Output array after bitwise_or:  [  3  11 127]

 
numpy.bitwise_xor() : This function is used to Compute the bit-wise XOR of two array element-wise. This function computes the bit-wise XOR of the underlying binary representation of the integers in the input arrays.

Code #1 :


# Python program explaining
# bitwise_xor() function

import numpy as geek
in_num1 = 10
in_num2 = 11

print ("Input  number1 : ", in_num1)
print ("Input  number2 : ", in_num2) 
  
out_num = geek.bitwise_xor(in_num1, in_num2) 
print ("bitwise_xor of 10 and 11 : ", out_num) 

Output :

Input  number1 :  10
Input  number2 :  11
bitwise_xor of 10 and 11 :  1

 
Code #2 :


# Python program explaining
# bitwise_xor() function

import numpy as geek

in_arr1 = [2, 8, 125]
in_arr2 = [3, 3, 115]
 
print ("Input array1 : ", in_arr1) 
print ("Input array2 : ", in_arr2)
  
out_arr = geek.bitwise_xor(in_arr1, in_arr2) 
print ("Output array after bitwise_xor: ", out_arr) 

Output :

Input array1 :  [2, 8, 125]
Input array2 :  [3, 3, 115]
Output array after bitwise_xor:  [ 1 11 14]

 
numpy.invert() : This function is used to Compute the bit-wise Inversion of an array element-wise. It computes the bit-wise NOT of the underlying binary representation of the integers in the input arrays.
For signed integer inputs, the two’s complement is returned. In a two’s-complement system negative numbers are represented by the two’s complement of the absolute value.

Code #1 :


# Python program explaining
# invert() function

import numpy as geek
in_num = 10
print ("Input  number : ", in_num)
  
out_num = geek.invert(in_num) 
print ("inversion of 10 : ", out_num) 

Output :

Input  number :  10
inversion of 10 :  -11

 
Code #2 :

# Python program explaining
# invert() function

import numpy as geek

in_arr = [2, 0, 25]
print ("Input array : ", in_arr)
  
out_arr = geek.invert(in_arr) 
print ("Output array after inversion: ", out_arr) 

Output :

Input array :  [2, 0, 25]
Output array after inversion:  [ -3  -1 -26]

 
numpy.left_shift() : This function is used to Shift the bits of an integer to the left.The bits are shifted to the left by appending arr2 0s(zeroes) at the right of arr1. Since the internal representation of numbers is in binary format, this operation is equivalent to multiplying arr1 by 2**arr2. For example, if the number is 5 and we want to 2 bit left shift then after left shift 2 bit the result will be 5*(2^2) = 20

Code #1 :


# Python program explaining
# left_shift() function

import numpy as geek
in_num = 5
bit_shift = 2

print ("Input  number : ", in_num)
print ("Number of bit shift : ", bit_shift ) 
  
out_num = geek.left_shift(in_num, bit_shift) 
print ("After left shifting 2 bit  : ", out_num) 

Output :

Input  number :  5
Number of bit shift :  2
After left shifting 2 bit  :  20

 
Code #2 :


# Python program explaining
# left_shift() function

import numpy as geek

in_arr = [2, 8, 15]
bit_shift =[3, 4, 5]
 
print ("Input array : ", in_arr) 
print ("Number of bit shift : ", bit_shift)
  
out_arr = geek.left_shift(in_arr, bit_shift) 
print ("Output array after left shifting: ", out_arr) 

Output :

Input array :  [2, 8, 15]
Number of bit shift :  [3, 4, 5]
Output array after left shifting:  [ 16 128 480]

 
numpy.right_shift() : This function is used to Shift the bits of an integer to the right.Because the internal representation of numbers is in binary format, this operation is equivalent to dividing arr1 by 2**arr2. For example, if the number is 20 and we want to 2-bit right shift then after right shift 2-bit the result will be 20/(2^2) = 5.

Code #1 :


# Python program explaining
# right_shift() function

import numpy as geek
in_num = 20
bit_shift = 2

print ("Input  number : ", in_num)
print ("Number of bit shift : ", bit_shift ) 
  
out_num = geek.right_shift(in_num, bit_shift) 
print ("After right shifting 2 bit  : ", out_num) 

Output :

Input  number :  20
Number of bit shift :  2
After right shifting 2 bit  :  5

 
Code #2 :


# Python program explaining
# right_shift() function

import numpy as geek

in_arr = [24, 48, 16]
bit_shift =[3, 4, 2]
 
print ("Input array : ", in_arr) 
print ("Number of bit shift : ", bit_shift)
  
out_arr = geek.right_shift(in_arr, bit_shift) 
print ("Output array after right shifting: ", out_arr) 

Output :

Input array :  [24, 48, 16]
Number of bit shift :  [3, 4, 2]
Output array after right shifting:  [3 3 4]

 
numpy.binary_repr(number, width=None) : This function is used to represent binary form of the input number as a string.For negative numbers, if width is not given, a minus sign is added to the front. If width is given, the two’s complement of the number is returned, with respect to that width.
In a two’s-complement system, negative numbers are represented by the two’s complement of the absolute value. This is the most common method of representing signed integers on computers.

Code #1 :


# Python program explaining
# binary_repr() function

import numpy as geek
in_num = 10

print ("Input  number : ", in_num)

out_num = geek.binary_repr(in_num) 
print ("binary representation of 10 : ", out_num) 

Output :

Input  number :  10
binary representation of 10 :  1010

 
Code #2 :

# Python program explaining
# binary_repr() function
import numpy as geek

in_arr = [5, -8 ]
 
print ("Input array : ", in_arr) 

# binary representation of first array  
# element without using width parameter
out_num = geek.binary_repr(in_arr[0])
print("Binary representation of 5")
print ("Without using width parameter : ", out_num) 

# binary representation of first array
# element using width parameter
out_num = geek.binary_repr(in_arr[0], width = 5)
print ("Using width parameter: ", out_num) 

print("\nBinary representation of -8")

# binary representation of 2nd array
# element without using width parameter
out_num = geek.binary_repr(in_arr[1])
print ("Without using width parameter : ", out_num) 

# binary representation of 2nd array
# element  using width parameter
out_num = geek.binary_repr(in_arr[1], width = 5)
print ("Using width parameter : ", out_num) 

Output :

Input array :  [5, -8]
Binary representation of 5 
Without using width parameter :  101
Using width parameter:  00101

Binary representation of -8  
Without using width parameter :  -1000
Using width parameter :  11000

numpy.packbits(myarray, axis=None) : This function is used to packs 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.

Code #1 :


# Python program explaining
# packbits() function
import numpy as np

# creating an array using 
# array function
a = np.array([[[1,0,1],
             [0,1,0]],
             [[1,1,0],
             [0,0,1]]])

# packing elements of an array
# using packbits() function
b = np.packbits(a, axis=-1)

print(b)

Output :

[[[160],[64]],[[192],[32]]]

 
numpy.unpackbits(myarray, axis=None) : This function is used to Unpacks elements of a uint8 array into a binary-valued output array.Each element of myarray 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.

Code #1 :


# Python program explaining
# unpackbits() function
import numpy as np

# creating an array using 
# array function
a = np.array([[2], [7], [23]], dtype=np.uint8)

# packing elements of an array
# using packbits() function
b = np.unpackbits(a, axis = 1)

print(b)

Output :

[[0, 0, 0, 0, 0, 0, 1, 0],
 [0, 0, 0, 0, 0, 1, 1, 1],
 [0, 0, 0, 1, 0, 1, 1, 1]]

 



  • Last Updated : 25 Jan, 2024

Share your thoughts in the comments
Similar Reads