The Wayback Machine - https://web.archive.org/web/20240930190756/https://www.geeksforgeeks.org/numpy-binary-operations/
Open In App

Numpy | Binary Operations

Last Updated : 12 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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
# 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
# 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
# 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
# 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
# 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
# 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
# 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
# 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
# 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
# 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
# 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
# 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
# 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
# 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
# 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
# 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]]

 



Previous Article
Next Article

Similar Reads

Arithmetic Operations on Images using OpenCV | Set-2 (Bitwise Operations on Binary Images)
Prerequisite: Arithmetic Operations on Images | Set-1Bitwise operations are used in image manipulation and used for extracting essential parts in the image. In this article, Bitwise operations used are : ANDORXORNOT Also, Bitwise operations helps in image masking. Image creation can be enabled with the help of these operations. These operations can
4 min read
Python | Numpy numpy.transpose()
With the help of Numpy numpy.transpose(), We can perform the simple function of transpose within one line by using numpy.transpose() method of Numpy. It can transpose the 2-D arrays on the other hand it has no effect on 1-D arrays. This method transpose the 2-D numpy array. Parameters: axes : [None, tuple of ints, or n ints] If anyone wants to pass
2 min read
Python | Numpy numpy.ndarray.__lt__()
With the help of numpy.ndarray.__lt__() method of Numpy, We can find that which element in an array is less than the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__lt__($self, value, /) Return: self<value Example #1 : In this example we can see that
1 min read
Python | Numpy numpy.ndarray.__gt__()
With the help of numpy.ndarray.__gt__() method of Numpy, We can find that which element in an array is greater then the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__gt__($self, value, /) Return: self>value Example #1 : In this example we can see th
1 min read
Python | Numpy numpy.ndarray.__le__()
With the help of numpy.ndarray.__le__() method of Numpy, We can find that which element in an array is less than or equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__le__($self, value, /) Return: self<=value Example #1 : In this example we
1 min read
Python | Numpy numpy.ndarray.__ge__()
With the help of numpy.ndarray.__ge__() method of Numpy, We can find that which element in an array is greater then or equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__ge__($self, value, /) Return: self>=value Example #1 : In this example
1 min read
Python | Numpy numpy.ndarray.__ne__()
With the help of numpy.ndarray.__ne__() method of Numpy, We can find that which element in an array is not equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__ne__($self, value, /) Return: self!=value Example #1 : In this example we can see that
1 min read
Python | Numpy numpy.ndarray.__neg__()
With the help of numpy.ndarray.__neg__() method of Numpy, one can multiply each and every element of an array with -1. Hence, the resultant array having values like positive values becomes negative and negative values become positive. Syntax: ndarray.__neg__($self, /) Return: -self Example #1 : In this example we can see that after applying numpy._
1 min read
Python | Numpy numpy.ndarray.__pos__()
With the help of numpy.ndarray.__pos__() method of Numpy, one can multiply each and every element of an array with 1. Hence, the resultant array having values same as original array. Syntax: ndarray.__pos__($self, /) Return: +self Example #1 : In this example we can see that after applying numpy.__pos__(), we get the simple array that can be the sa
1 min read
Python | Numpy numpy.ndarray.__sub__()
With the help of Numpy numpy.ndarray.__sub__(), We can subtract a particular value that is provided as a parameter in the ndarray.__sub__() method. Value will be subtracted to each and every element in a numpy array. Syntax: ndarray.__sub__($self, value, /) Return: self-value Example #1 : In this example we can see that each and every element in an
1 min read
Python | Numpy numpy.ndarray.__add__()
With the help of Numpy numpy.ndarray.__add__(), we can add a particular value that is provided as a parameter in the ndarray.__add__() method. Value will be added to each and every element in a numpy array. Syntax: ndarray.__add__($self, value, /) Return: self+value Example #1 : In this example we can see that each and every element in an array is
1 min read
Python | Numpy numpy.ndarray.__floordiv__()
With the help of Numpy numpy.ndarray.__floordiv__(), one can divide a particular value that is provided as a parameter in the ndarray.__floordiv__() method. Value will be divided to each and every element in a numpy array and remember it always gives the floor value after division. Syntax: ndarray.__floordiv__($self, value, /) Return: self//value E
1 min read
Python | Numpy numpy.ndarray.__mod__()
With the help of Numpy numpy.ndarray.__mod__(), every element in an array is operated on binary operator i.e mod(%). Remember we can use any type of values in an array and value for mod is applied as the parameter in ndarray.__mod__(). Syntax: ndarray.__mod__($self, value, /) Return: self%value Example #1 : In this example we can see that value tha
1 min read
Python | Numpy numpy.ndarray.__divmod__()
With the help of Numpy numpy.ndarray.__divmod__() method, we will get two arrays one is having elements that is divided by value that is provided in numpy.ndarray.__divmod__() method and second is having elements that perform mod operation with same value as provided in numpy.ndarray.__divmod__() method. Syntax: ndarray.__divmod__($self, value, /)
1 min read
Python | Numpy numpy.ndarray.__rshift__()
With the help of numpy.ndarray.__rshift__() method, we can get the elements that is right shifted by the value that is provided as a parameter in numpy.ndarray.__rshift__() method. Syntax: ndarray.__rshift__($self, value, /) Return: self>>value Example #1 : In this example we can see that every element is right shifted by the value that is pa
1 min read
Python | Numpy numpy.ndarray.__lshift__()
With the help of Numpy numpy.ndarray.__lshift__() method, we can get the elements that is left shifted by the value that is provided as a parameter in numpy.ndarray.__lshift__() method. Syntax: ndarray.__lshift__($self, value, /) Return: self<<value Example #1 : In this example we can see that every element is left shifted by the value that i
1 min read
Python | Numpy numpy.ndarray.__imul__()
With the help of numpy.ndarray.__imul__() method, we can multiply a particular value that is provided as a parameter in the ndarray.__imul__() method. Value will be multiplied to every element in a numpy array. Syntax: ndarray.__imul__($self, value, /)Return: self*=value Example #1 : In this example we can see that each and every element in an arra
1 min read
Python | Numpy numpy.ndarray.__isub__()
With the help of numpy.ndarray.__isub__() method, we can subtract a particular value that is provided as a parameter in the ndarray.__isub__() method. Value will be subtracted to every element in a numpy array. Syntax: ndarray.__isub__($self, value, /) Return: self-=value Example #1 : In this example we can see that each and every element in an arr
1 min read
Python | Numpy numpy.ndarray.__iadd__()
With the help of numpy.ndarray.__iadd__() method, we can add a particular value that is provided as a parameter in the ndarray.__iadd__() method. Value will be added to every element in a numpy array. Syntax: ndarray.__iadd__($self, value, /) Return: self+=value Example #1 : In this example we can see that each and every element in an array is adde
1 min read
Python | Numpy numpy.ndarray.__xor__()
With the help of Numpy numpy.ndarray.__xor__() method, we can get the elements that is XOR by the value that is provided as a parameter in numpy.ndarray.__xor__() method. Syntax: ndarray.__xor__($self, value, /) Return: self^value Example #1 : In this example we can see that every element is xor by the value that is passed as a parameter in ndarray
1 min read
Python | Numpy numpy.ndarray.__and__()
With the help of Numpy numpy.ndarray.__and__() method, we can get the elements that is anded by the value that is provided as a parameter in numpy.ndarray.__and__() method. Syntax: ndarray.__and__($self, value, /) Return: self&value Example #1 : In this example we can see that every element is anded by the value that is passed as a parameter in
1 min read
Python | Numpy numpy.ndarray.__or__()
With the help of Numpy numpy.ndarray.__or__() method, we can get the elements that is OR by the value that is provided as a parameter in numpy.ndarray.__or__() method. Syntax: ndarray.__or__($self, value, /) Return: self|value Example #1 : In this example we can see that every element is or by the value that is passed as a parameter in ndarray.__or
1 min read
Python | Numpy numpy.matrix.any()
With the help of Numpy numpy.matrix.any() method, we are able to compare each and every element of one matrix with another or we can provide the axis on the we want to apply comparison if any of the element matches it return true. Syntax : numpy.matrix.any() Return : Return true if any match found else false Example #1 : In this example we can see
1 min read
Python | Numpy numpy.matrix.all()
With the help of Numpy numpy.matrix.all() method, we are able to compare each and every element of one matrix with another or we can provide the axis on the we want to apply comparison. Syntax : numpy.matrix.all() Return : Return true if found match else false Example #1 : In this example we can see that with the help of matrix.all() method, we are
1 min read
Python | Numpy numpy.matrix.A()
With the help of Numpy numpy.matrix.A() method, we can get the same matrix as self. It means through this method we can get the identical matrix. Syntax : numpy.matrix.A() Return : Return self matrix Example #1 : In this example we can see that with the help of matrix.A() method, we are able to get the self matrix. # import the important module in
1 min read
Python | Numpy numpy.matrix.T()
With the help of Numpy numpy.matrix.T() method, we can make a Transpose of any matrix either having dimension one or more than more. Syntax : numpy.matrix.T() Return : Return transpose of every matrix Example #1 : In this example we can see that with the help of matrix.T() method, we are able to transform any type of matrix. # import the important
1 min read
Python | Numpy numpy.matrix.H()
With the help of Numpy numpy.matrix.H() method, we can make a conjugate Transpose of any complex matrix either having dimension one or more than more. Syntax : numpy.matrix.H() Return : Return conjugate transpose of every complex matrix Example #1 : In this example we can see that with the help of matrix.H() method, we are able to transform any typ
1 min read
Python | Numpy numpy.choose()
With the help of Numpy numpy.choose() method, we can select the elements from an multidimensional array by passing a parameter as an array which contain the index of row number to be selected. Output array having the same size as passed in the parameter. Syntax : numpy.choose() Return : Return an array of element choice Example #1 : In this example
1 min read
Difference between Numpy array and Numpy matrix
While working with Python many times we come across the question that what exactly is the difference between a numpy array and numpy matrix, in this article we are going to read about the same. What is np.array() in PythonThe Numpy array object in Numpy is called ndarray. We can create ndarray using numpy.array() function. It is used to convert a l
3 min read
NumPy Array Sorting | How to sort NumPy Array
Sorting an array is a very important step in data analysis as it helps in ordering data, and makes it easier to search and clean. In this tutorial, we will learn how to sort an array in NumPy. You can sort an array in NumPy: Using np.sort() functionin-line sortsorting along different axesUsing np.argsort() functionUsing np.lexsort() functionUsing s
4 min read
Article Tags :
Practice Tags :