NumPy Bitwise Operations
Bitwise operations are a type of operation performed at the bit level of binary numbers; they operate directly on the individual bits of a binary number, without regard to the overall value of the number.
NumPy provides a series of bitwise functions that allow bit-by-bit operations on the elements of an array. These operations are similar to Python's bitwise operators, but act on NumPy arrays, supporting vectorized processing with higher performance.
Bitwise operations are widely used in computer science for optimization and processing of low-level data.
Functions in NumPy beginning with bitwise_ are bitwise operation functions.
NumPy bitwise operations include the following functions:
| Operation | Function/Operator | Description |
|---|---|---|
| Bitwise AND | numpy.bitwise_and(x1, x2) |
Perform a bitwise AND operation on each element of the array. |
| Bitwise OR | numpy.bitwise_or(x1, x2) |
Perform a bitwise OR operation on each element of the array. |
| Bitwise XOR | numpy.bitwise_xor(x1, x2) |
Perform a bitwise XOR operation on each element of the array. |
| Bitwise NOT | numpy.invert(x) |
Perform a bitwise inversion (bitwise NOT) on each element of the array. |
| Left shift | numpy.left_shift(x1, x2) |
Shift each element of the array left by the specified number of bits. |
| Right shift | numpy.right_shift(x1, x2) |
Shift each element of the array right by the specified number of bits. |
Example
import
numpy
as
np
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
)
(
"AND:"
,
result_and
)
[False, False, False]
(
"OR:"
,
result_or
)
[True, True, True]
(
"XOR:"
,
result_xor
)
[True, True, True]
(
"NOT:"
,
result_not
)
[False, True, False]
Bitwise NOT
arr_invert
=
np.
invert
(
np.
array
(
[
1
,
2
]
,
dtype
=
np.
int8
)
)
(
"Invert:"
,
arr_invert
)
[-2, -3]
Left shift operation
arr_left_shift
=
np.
left_shift
(
5
,
2
)
(
"Left Shift:"
,
arr_left_shift
)
20
Right shift operation
arr_right_shift
=
np.
right_shift
(
10
,
1
)
(
"Right Shift:"
,
arr_right_shift
)
5
You can also use "&", "~", "|" and "^" and other operators for calculation:
-
AND operation (&): When both digits at the corresponding bit are 1, the result is 1; otherwise, the result is 0.
For example: 1010 & 1100 = 1000
-
OR operation (|): When one of the two digits at the corresponding bit is 1, the result is 1; otherwise, the result is 0.
For example: 1010 | 1100 = 1110
-
XOR operation (^): When the two digits at the corresponding bit differ, 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, i.e. 0 becomes 1 and 1 becomes 0.
For example: ~1010 = 0101
-
Left shift operation (<<): Move all bits of the number to the left by the specified number of bits, filling the right side with 0s.
For example: 1010 << 2 = 101000
-
Right shift operation (>>): Move all bits of the number to the 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 the integers in an array.
Example
import
numpy
as
np
(
'
Binary forms of 13 and 17:
'
)
a
,
b
=
13
,
17
(
bin
(
a
)
,
bin
(
b
)
)
(
'
\n
'
)
(
'
Bitwise AND of 13 and 17:
'
)
(
np
.
bitwise_and
(
13
,
17
)
)
The output is:
13 和 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 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 the integers in an array.
Example
import
numpy
as
np
a
,
b
=
13
,
17
(
'
Binary forms of 13 and 17:
'
)
(
bin
(
a
)
,
bin
(
b
)
)
(
'
Bitwise OR of 13 and 17:
'
)
(
np
.
bitwise_or
(
13
,
17
)
)
The output is:
13 和 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 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 inversion on the integers in an array, i.e. 0 becomes 1 and 1 becomes 0.
For signed integers, take the complement of the binary number, then +1. For a binary number, the highest bit of 0 indicates a positive number and the highest bit of 1 indicates a negative number.
Let's look at the calculation steps for ~1:
- Convert **
1** (called the original code here) to binary =00000001 - Bitwise inversion = **
11111110** - Find that the sign bit (i.e. the highest bit) is **
1** (indicating a negative number), invert the digits other than the sign bit =10000001 - Add 1 to the last bit to take its complement =
10000010 - Convert back to decimal =
-2
Example
import
numpy
as
np
(
'
Bitwise inversion of 13, where the ndarray's dtype is uint8:
'
)
(
np
.
invert
(
np
.
array
(
[
13
]
,
dtype
=
np
.
uint8
)
)
)
(
'
\n
'
)
Comparing the binary representations of 13 and 242, we find the inversion of bits
(
'
Binary representation of 13:
'
)
(
np
.
binary_repr
(
13
,
width
=
8
)
)
(
'
\n
'
)
(
'
Binary representation of 242:
'
)
(
np
.
binary_repr
(
242
,
width
=
8
)
)
The output is:
13 的位反转,其中 ndarray 的 dtype 是 uint8:
[242]
Binary representation of 13:
00001101
Binary representation of 242:
11110010
left_shift
The left_shift() function shifts the binary form of array elements to the left to the specified position, appending an equal number of 0s on the right.
Example
import
numpy
as
np
(
'
Shift 10 left by two bits:
'
)
(
np
.
left_shift
(
10
,
2
)
)
(
'
\n
'
)
(
'
Binary representation of 10:
'
)
(
np
.
binary_repr
(
10
,
width
=
8
)
)
(
'
\n
'
)
(
'
Binary representation of 40:
'
)
(
np
.
binary_repr
(
40
,
width
=
8
)
)
The two bits in '00001010' have moved to the left, and two 0s have been added on the right.
The output is:
将 10 左移两位:
40
Binary representation of 10:
00001010
Binary representation of 40:
00101000
right_shift
The right_shift() function shifts the binary form of array elements to the right to the specified position, appending an equal number of 0s on the left.
Example
import
numpy
as
np
(
'
Shift 40 right by two bits:
'
)
(
np
.
right_shift
(
40
,
2
)
)
(
'
\n
'
)
(
'
Binary representation of 40:
'
)
(
np
.
binary_repr
(
40
,
width
=
8
)
)
(
'
\n
'
)
(
'
Binary representation of 10:
'
)
(
np
.
binary_repr
(
10
,
width
=
8
)
)
The two bits in '00001010' have moved to the right, and two 0s have been added on the left.
The output is:
将 40 右移两位:
10
Binary representation of 40:
00101000
Binary representation of 10:
00001010