Binary Operations in NumPy:

The binary operators act on the bits and perform bit by bit operation. Binary operation is a rule for combination of two values for creation of a new value in python for data science.

numpy.bitwise_and(): It is a function for computing the bitwise AND of two array elements. So, it computes the bit-wise AND of underlying binary representation of integers in the input arrays.

Code 1:

Binary Operations in NumPy Python for Data Science - PST Analytics

Code 2:

Binary Operations in NumPy Python for Data Science - PST Analytics

numpy.bitwise_or():

It is a function for computing the bit-wise OR of two array element wise. So, it is a function which will compute the bit-wise OR of underlying binary representation of integers in input arrays.

Code 1:

Code 2:

Binary Operations in NumPy Python for Data Science - PST Analytics

numpy.bitwise_xor(): It is a function for computing the bit-wise XOR of two array element wise. So, this function will compute the bit-wise XOR of underlying binary representation of integers in input arrays.

Code 1:

Code 2:

numpy.invert(): It is a function for computing bit-wise inversion of array element wise. It will compute the bit-wise NOT of underlying binary representation of integers in input array. So, for signed integer inputs, two’s complement will be return. In system of a two’s complement negative numbers will be represent by two’s complement of the absolute value.

Code 1:

Binary Operations in NumPy Python for Data Science - PST Analytics

Code 2:

Binary Operations in NumPy Python for Data Science - PST Analytics

numpy.left_shift():

It is a function which is there for shifting the bits of an integer to the left. The bits are shift left by appending arr2 zeroes at the right of arr1. The internal representation of the numbers is in binary format so, this operation is equivalent to multiplying arr1 by 2**arr2.

Code 1:

Code 2:

numpy.right_shift(): It is a function which is there for shifting the bits of integer to the right. The internal representation of the numbers is in binary format so, this operation is equivalent to multiplying arr1 by 2**arr2.

Code 1:

 

Code 2:

numpy.binary_repr(numbers, width=None): It is a function for representing the binary form of the input number as string. In case of negative numbers when width is not there a minus sign is added to the front. When width is given, the two’s complement of the number will be returned with respect to the width.

In system of two’s complement negative numbes are represent by two’s complement of the absolute value. It is a common method of representing sign integers on computers.

Code 1:

Code 2:

numpy.packbits(myarray,axis=None): It is there for packing the elements of a binary valued array into bits in a unit8 array. The result will be pad to full bytes by inserting zero bits at the end.

Code 1:

Numpy.unpackbits(myarrays,axis=None): It is a function for unpacking elements of a unit8 array into a binary valued output array. Each element in myarray represents a bit-field which should be unpacked into binary valued output array. The shape of the output array will either be 1-D or shape of the input array.

Code 1:

So, to learn more abouot binary operations in python for data science, you can check this and this as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.