NumPy Bitwise Operators

Numpy provides the following bitwise operators.

SN Operator Description
1 bitwise_and It is used to calculate the bitwise and operation between the corresponding array elements.
2 bitwise_or It is used to calculate the bitwise or operation between the corresponding array elements.
3 invert It is used to calculate the bitwise not the operation of the array elements.
4 left_shift It is used to shift the bits of the binary representation of the elements to the left.
5 right_shift It is used to shift the bits of the binary representation of the elements to the right.

bitwise_and Operation

The NumPy provides the bitwise_and() function which is used to calculate the bitwise_and operation of the two operands.

The bitwise and operation is performed on the corresponding bits of the binary representation of the operands. If both the corresponding bit in the operands is set to 1, then only the resultant bit in the AND result will be set to 1 otherwise it will be set to 0.

Example

snippet
import numpy as np

a = 10
b = 12

print("binary representation of a:",bin(a))
print("binary representation of b:",bin(b))
print("Bitwise-and of a and b: ",np.bitwise_and(a,b))

Output:

Output
binary representation of a: 0b1010 binary representation of b: 0b1100 Bitwise-and of a and b: 8

AND Truth Table

The output of the AND result of the two bits is 1 if and only if both the bits are 1 otherwise it will be 0.

A B AND (A, B)
0 0 0
0 1 0
1 0 0
1 1 1

bitwise_or Operator

The NumPy provides the bitwise_or() function which is used to calculate the bitwise or operation of the two operands.

The bitwise or operation is performed on the corresponding bits of the binary representation of the operands. If one of the corresponding bit in the operands is set to 1 then the resultant bit in the OR result will be set to 1; otherwise it will be set to 0.

Example

snippet
import numpy as np

a = 50
b = 90
print("binary representation of a:",bin(a))
print("binary representation of b:",bin(b))
print("Bitwise-or of a and b: ",np.bitwise_or(a,b))

Output:

Output
binary representation of a: 0b110010 binary representation of b: 0b1011010 Bitwise-or of a and b: 122

Or truth table

The output of the OR result of the two bits is 1 if one of the bits are 1 otherwise it will be 0.

A B Or (A, B)
0 0 0
0 1 1
1 0 1
1 1 1

Invert operation

It is used to calculate the bitwise not the operation of the given operand. The 2's complement is returned if the signed integer is passed in the function.

Consider the following example.

Example

snippet
import numpy as np

arr = np.array([20],dtype = np.uint8)
print("Binary representation:",np.binary_repr(20,8))

print(np.invert(arr))

print("Binary representation: ", np.binary_repr(235,8))

Output:

Output
Binary representation: 00010100 [235] Binary representation: 11101011

It shifts the bits in the binary representation of the operand to the left by the specified position. An equal number of 0s are appended from the right. Consider the following example.

Example

snippet
import numpy as np

print("left shift of 20 by 3 bits",np.left_shift(20, 3))

print("Binary representation of 20 in 8 bits",np.binary_repr(20, 8))

print("Binary representation of 160 in 8 bits",np.binary_repr(160,8))

Output:

Output
left shift of 20 by 3 bits 160 Binary representation of 20 in 8 bits 00010100 Binary representation of 160 in 8 bits 10100000

Right Shift Operation

It shifts the bits in the binary representation of the operand to the right by the specified position. An equal number of 0s are appended from the left. Consider the following example.

Example

snippet
import numpy as np

print("left shift of 20 by 3 bits",np.right_shift(20, 3))

print("Binary representation of 20 in 8 bits",np.binary_repr(20, 8))

print("Binary representation of 160 in 8 bits",np.binary_repr(160,8))

Output:

Output
left shift of 20 by 3 bits 2 Binary representation of 20 in 8 bits 00010100 Binary representation of 160 in 8 bits 10100000
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +