Javatpoint Logo
Javatpoint Logo

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

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

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

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

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

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





Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA