Numpy | Binary Operations - GeeksforGeeks

15 Nov, 2018
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 :
Output :
Input  number1 :  10
Input  number2 :  11
bitwise_and of 10 and 11 :  10

 Code #2 :
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 :
Output :
Input  number1 :  10
Input  number2 :  11
bitwise_or of 10 and 11 :  11

 Code #2 :
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 :
Output :
Input  number1 :  10
Input  number2 :  11
bitwise_xor of 10 and 11 :  1

 Code #2 :
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 :
Output :
Input  number :  10
inversion of 10 :  -11

 Code #2 :
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 :
Output :
Input  number :  5
Number of bit shift :  2
After left shifting 2 bit  :  20

 Code #2 :
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 :
Output :
Input  number :  20
Number of bit shift :  2
After right shifting 2 bit  :  5

 Code #2 :
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 :
Output :
Input  number :  10
binary representation of 10 :  1010

 Code #2 :
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 :
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 :
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]]

 
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Must Do Coding Questions for Companies like Amazon, Microsoft, Adobe, ...
Learn C++ Programming Step by Step - A 20 Day Curriculum!
Must Do Coding Questions for Product Based Companies
50 Common Ports You Should Know
GeeksforGeeks Jobathon - Are You Ready For This Hiring Challenge?
How to Find Length of String in Bash Script?
Samsung R&D Internship Interview Experience (On-Campus)
Generate UUID in Golang
How to install PHP in windows 10 ?
Types of Distributed System