Table of Contents
ToggleBinary 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_andComputes bitwise AND operation of array elements |
| 2 | bitwise_orComputes bitwise OR operation of array elements |
| 3 | bitwise_xorComputes bitwise XOR of array elements. Each bit of the result is 1 if the corresponding bits in the input are different. |
| 4 | left_shiftShifts bits of a binary representation to the left |
| 5 | right_shiftShifts bits of binary representation to the right |
| 6 | bitwise_right_shiftShifts the bits of an integer to the right. |
| 7 | invertComputes bitwise NOT |
| 8 | bitwise_invertComputes bitwise inversion of the elements. |
| 9 | packbitsPacks the elements of a binary array into packed bitfield representation. |
| 10 | unpackbitsUnpacks elements of a binary array into a list of bits. |
| 11 | binary_reprConverts an integer to its binary representation as a string. |
The bitwise AND operation compares each bit of two numbers. If both bits are “1”, the result is “1”; otherwise, it is “0” −
# Open Compiler
# (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)
Output:
Following is the output obtained −
The result obtained is: 1
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”. −
# Open Compiler
# (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)
Output:
This will produce the following result −
The result obtained is: 7
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 −
# Open Compiler
# (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)
Output:
Following is the output of the above code −
The result obtained is: -6
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 −
# Open Compiler
# (0101 in binary)
a = 5
# (1010 in binary, which is 10 in decimal)
result = a << 1
print("The result obtained is:",result)
Output:
The output obtained is as shown below −
The result obtained is: 10
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) −
# Open Compiler
# (0101 in binary)
a = 5
# (0010 in binary, which is 2 in decimal)
result = a >> 1
print("The result obtained is:",result)
Output:
After executing the above code, we get the following output −
The result obtained is: 2
Key Takeaway: Master binary operators in NumPy with Vista Academy!
