NumPy Bitwise Operations
Bitwise operations are a class of operations performed at the bit level of binary numbers. They directly operate on the individual bits of binary numbers without considering the overall value of the number.
NumPy provides a series of bitwise operation functions that allow bitwise operations on elements in arrays. These operations are similar to Python's bitwise operators, but they act on NumPy arrays, support vectorized processing, and offer higher performance.
Bitwise operations are widely used in computer science for optimization and processing of low-level data.
NumPy bitwise_The functions starting with "bitwise" are bitwise operation functions.
NumPy bitwise operations include the following functions:
| Operation | Function/Operator | Description |
|---|---|---|
| Bitwise AND | numpy.bitwise_and(x1, x2) | Performs a bitwise AND operation on each element of the array. |
| Bitwise OR | numpy.bitwise_or(x1, x2) | Performs a bitwise OR operation on each element of the array. |
| Bitwise XOR | numpy.bitwise_xor(x1, x2) | Performs a bitwise XOR operation on each element of the array. |
| Bitwise NOT | numpy.invert(x) | Performs a bitwise NOT operation on each element of the array. |
| Left shift | numpy.left_shift(x1, x2) | Shifts each element of the array left by the specified number of bits. |
| Right shift | numpy.right_shift(x1, x2) | Shifts each element of the array right by the specified number of bits. |
Example
arr1 = np.array([True, False, True], dtype=bool)
arr2 = np.array([False, True, False], dtype=bool)
result_and = np.bitwise_and(arr1, arr2)
result_or = np.bitwise_or(arr1, arr2)
result_xor = np.bitwise_xor(arr1, arr2)
result_not = np.bitwise_not(arr1)
print("AND:", result_and) # [False, False, False]
print("OR:", result_or) # [True, True, True]
print("XOR:", result_xor) # [True, True, True]
print("NOT:", result_not) # [False, True, False]
# Bitwise NOT
arr_invert = np.invert(np.array([1, 2], dtype=np.int8))
print("Invert:", arr_invert) # [-2, -3]
# Left shift operation
arr_left_shift = np.left_shift(5, 2)
print("Left Shift:", arr_left_shift) # 20
# Right shift operation
arr_right_shift = np.right_shift(10, 1)
print("Right Shift:", arr_right_shift) # 5
You can also use"&", "~", "|" and "^"and other operators to perform calculations:
AND operation (&):When both digits on the corresponding bits are 1, the result is 1; otherwise, the result is 0.
For example: 1010 & 1100 = 1000
OR operation (|):When at least one of the two digits on the corresponding bits is 1, the result is 1; otherwise, the result is 0.
For example: 1010 | 1100 = 1110
XOR operation (^):When the two digits on the corresponding bits are different, the result is 1; when they are the same, the result is 0.
For example: 1010 ^ 1100 = 0110
NOT operation (~):Invert each bit of the number, that is, 0 becomes 1 and 1 becomes 0.
For example: ~1010 = 0101
Left shift operation (<<):Shift all bits of the number left by the specified number of bits, filling the right side with 0s.
For example: 1010 << 2 = 101000
Right shift operation (>>):Shift all bits of the number right by the specified number of bits, filling the left side according to the sign bit or with zeros.
For example: 1010 >> 2 = 0010
bitwise_and
The bitwise_and() function performs a bitwise AND operation on the binary form of integers in an array.
Example
The output result is:
13 和 17 的二进制形式: 0b1101 0b10001 13 和 17 的位与: 1
The above example can be illustrated with the following table:
| 1 | 1 | 0 | 1 | ||
|---|---|---|---|---|---|
| AND | |||||
| 1 | 0 | 0 | 0 | 1 | |
| Operation result | 0 | 0 | 0 | 0 | 1 |
The rules of the bitwise AND operation are as follows:
| A | B | AND |
|---|---|---|
| 1 | 1 | 1 |
| 1 | 0 | 0 |
| 0 | 1 | 0 |
| 0 | 0 | 0 |
bitwise_or
The bitwise_or() function performs a bitwise OR operation on the binary form of integers in an array.
Example
The output result is:
13 和 17 的二进制形式: 0b1101 0b10001 13 和 17 的位或: 29
The above example can be illustrated with the following table:
| 1 | 1 | 0 | 1 | ||
|---|---|---|---|---|---|
| OR | |||||
| 1 | 0 | 0 | 0 | 1 | |
| Operation result | 1 | 1 | 1 | 0 | 1 |
The rules of the bitwise OR operation are as follows:
| A | B | OR |
|---|---|---|
| 1 | 1 | 1 |
| 1 | 0 | 1 |
| 0 | 1 | 1 |
| 0 | 0 | 0 |
invert
The invert() function performs a bitwise NOT operation on integers in an array, that is, 0 becomes 1 and 1 becomes 0.
For signed integers, take the complement of the binary number, then add 1. In a binary number, the most significant bit being 0 indicates a positive number, and the most significant bit being 1 indicates a negative number.
Let's look at the calculation steps of ~1:
- will
1(This is called: original code) Convert to binary =00000001 - Bitwise inversion =
11111110 - We find that the sign bit (i.e., the most significant bit) is
1(indicating a negative number), invert the other digits except the sign bit =10000001 - Add 1 to the last bit to get its complement =
10000010 - Convert back to decimal =
-2
| Expression | Binary value (two's complement) | Decimal value |
|---|---|---|
| 5 | 00000000 00000000 00000000 00000101 | 5 |
| ~5 | 11111111 11111111 11111111 11111010 | -6 |
Example
The output result is:
13 的位反转,其中 ndarray 的 dtype 是 uint8: [242] 13 的二进制表示: 00001101 242 的二进制表示: 11110010
left_shift
The left_shift() function shifts the binary form of array elements to the left by a specified number of positions, appending an equal number of 0s on the right.
Example
The output result is:
将 10 左移两位: 40 10 的二进制表示: 00001010 40 的二进制表示: 00101000
right_shift
The right_shift() function shifts the binary form of array elements to the right by a specified number of positions, appending an equal number of 0s on the left.
Example
The output result is:
将 40 右移两位: 10 40 的二进制表示: 00101000 10 的二进制表示: 00001010Other extensions