Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

NumPy - Binary Operators



Binary Operators in NumPy

Binary operators in NumPy are operations that take two operands (usually arrays) and perform element-wise operations between corresponding elements of the arrays. These operations include addition, subtraction, multiplication, division, logical operations, and more.

For example, if you have two arrays, you can add them together, subtract one from the other, multiply their elements, and so on. Each of these operations is performed element-wise, meaning that the operation is applied to each corresponding pair of elements in the two arrays.

Following are the functions for bitwise operations available in NumPy package.

Sr.No. Operation & Description
1 bitwise_and

Computes bitwise AND operation of array elements

2 bitwise_or

Computes bitwise OR operation of array elements

3 bitwise_xor

Computes bitwise XOR of array elements. Each bit of the result is 1 if the corresponding bits in the input are different.

4 left_shift

Shifts bits of a binary representation to the left

5 right_shift

Shifts bits of binary representation to the right

6 bitwise_right_shift

Shifts the bits of an integer to the right.

7 invert

Computes bitwise NOT

8 bitwise_invert

Computes bitwise inversion of the elements.

9 packbits

Packs the elements of a binary array into packed bitfield representation.

10 unpackbits

Unpacks elements of a binary array into a list of bits.

11 binary_repr

Converts an integer to its binary representation as a string.

The Bitwise AND (&) Operation

The bitwise AND operation compares each bit of two numbers. If both bits are "1", the result is "1"; otherwise, it is "0" −

# (0101 in binary)
a = 5
# (0011 in binary)
b = 3
# (0001 in binary, which is 1 in decimal)
result = a & b  
print("The result obtained is:",result)  

Following is the output obtained −

The result obtained is: 1

The Bitwise OR (|) Operation

The bitwise OR operation compares each bit of two numbers. If either bit is "1", the result is "1"; if both are "0", the result is "0". −

# (0101 in binary)
a = 5       
# (0011 in binary)
b = 3       
# (0111 in binary, which is 7 in decimal)
result = a | b  
print("The result obtained is:",result)  

This will produce the following result −

The result obtained is: 7

The Bitwise NOT (~) Operation

The bitwise NOT operation inverts each bit of the number, turning "0" into "1" and "1" into "0". This is also known as the bitwise complement −

# (0101 in binary)
a = 5      
# (1010 in binary, which is -6 in decimal with two's complement)
result = ~a  
print("The result obtained is:",result)  

Following is the output of the above code −

The result obtained is: -6

The Left Shift (<<) Operation

The left shift operation shifts the bits of the number to the left by a specified number of positions. Bits shifted out on the left are discarded, and 0s are shifted in on the right −

# (0101 in binary)
a = 5    
# (1010 in binary, which is 10 in decimal)
result = a << 1  
print("The result obtained is:",result)  

The output obtained is as shown below −

The result obtained is: 10

The Right Shift (>>) Opeartion

The right shift operation shifts the bits of the number to the right by a specified number of positions. Bits shifted out on the right are discarded, and the leftmost bits are filled based on the sign of the number (arithmetic shift for signed integers) −

# (0101 in binary)
a = 5    
# (0010 in binary, which is 2 in decimal)
result = a >> 1  
print("The result obtained is:",result)  

After executing the above code, we get the following output −

The result obtained is: 2
Advertisements