NumPy Bitwise Operations
NumPy functions starting with "bitwise_" are bitwise operation functions.
NumPy bitwise operations include the following functions:
Function | Description |
---|---|
bitwise_and | Performs bitwise AND operation on array elements |
bitwise_or | Performs bitwise OR operation on array elements |
invert | Bitwise inversion |
left_shift | Shifts bits of binary representation to the left |
right_shift | Shifts bits of binary representation to the right |
Note: You can also use operators such as "&", "~", "|", and "^" for calculations.
bitwise_and
The bitwise_and() function performs bitwise AND operation on the binary forms of integers in an array.
Example
import numpy as np
print('Binary forms of 13 and 17:')
a, b = 13, 17
print(bin(a), bin(b))
print('\n')
print('Bitwise AND of 13 and 17:')
print(np.bitwise_and(13, 17))
Output:
Binary forms of 13 and 17:
0b1101 0b10001
Bitwise AND of 13 and 17:
1
The above example can be illustrated with the following table:
1 | 1 | 0 | 1 | |||
---|---|---|---|---|---|---|
AND | 1 | 0 | 0 | 0 | 1 | |
Result | 0 | 0 | 0 | 0 | 1 |
The bitwise AND operation follows these rules:
A | B | AND |
---|---|---|
1 | 1 | 1 |
1 | 0 | 0 |
0 | 1 | 0 |
0 | 0 | 0 |
bitwise_or
The bitwise_or() function performs bitwise OR operation on the binary forms of integers in an array.
Example
import numpy as np
a, b = 13, 17
print('Binary forms of 13 and 17:')
print(bin(a), bin(b))
print('Bitwise OR of 13 and 17:')
print(np.bitwise_or(13, 17))
Output:
Binary forms of 13 and 17:
0b1101 0b10001
Bitwise OR of 13 and 17:
29
The above example can be illustrated with the following table:
1 | 1 | 0 | 1 | |||
---|---|---|---|---|---|---|
OR | 1 | 0 | 0 | 0 | 1 | |
Result | 1 | 1 | 1 | 0 | 1 |
The bitwise OR operation follows these rules:
A | B | OR |
---|---|---|
1 | 1 | 1 |
1 | 0 | 1 |
0 | 1 | 1 |
0 | 0 | 0 |
invert
The invert() function performs bitwise inversion on integers in an array, i.e., 0 becomes 1 and 1 becomes 0.
For signed integers, it takes the complement of the binary number and then adds 1. The highest bit of a binary number indicates its sign, with 0 being positive and 1 being negative.
Here are the steps for calculating ~1:
- Convert the original number (called the "true form") to binary.
- Bitwise inversion.
- Since the sign bit (the highest bit) is 1 (indicating a negative number), invert the other bits.
- Add 1 to get the two's complement.
- Convert back to decimal.
Expression | Binary Value (2's complement) | Decimal Value |
---|---|---|
5 | 00000000 00000000 00000000 00000101 | 5 |
~5 | 11111111 11111111 11111111 11111010 | -6 |
Example
import numpy as np
print('Bitwise inversion of 13 with dtype uint8:')
print(np.invert(np.array([13], dtype=np.uint8)))
print('\n')
# Compare the binary representations of 13 and 242 to see the inversion
print('Binary representation of 13:')
print(np.binary_repr(13, width=8))
print('\n')
print('Binary representation of 242:')
print(np.binary_repr(242, width=8))
Output:
Bitwise inversion of 13 with dtype uint8:
[242]
Binary representation of 13:
00001101
Binary representation of 242:
11110010
left_shift
The left_shift()
function shifts the binary representation of array elements to the left by a specified number of positions, appending an equal number of zeros to the right.
Example
import numpy as np
print('Shifting 10 left by two positions:')
print(np.left_shift(10, 2))
print('\n')
print('Binary representation of 10:')
print(np.binary_repr(10, width=8))
print('\n')
print('Binary representation of 40:')
print(np.binary_repr(40, width=8))
# Two bits from '00001010' are moved to the left, and two zeros are added to the right.
Output:
Shifting 10 left by two positions:
40
Binary representation of 10:
00001010
Binary representation of 40:
00101000
right_shift
The right_shift()
function shifts the binary representation of array elements to the right by a specified number of positions, appending an equal number of zeros to the left.
Example
import numpy as np
print('Shifting 40 right by two positions:')
print(np.right_shift(40, 2))
print('\n')
print('Binary representation of 40:')
print(np.binary_repr(40, width=8))
print('\n')
print('Binary representation of 10:')
print(np.binary_repr(10, width=8))
# Two bits from '00101000' are moved to the right, and two zeros are added to the left.
Output:
Shifting 40 right by two positions:
10
Binary representation of 40:
00101000
Binary representation of 10:
00001010