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:

OperationFunction/OperatorDescription
Bitwise ANDnumpy.bitwise_and(x1, x2)Performs a bitwise AND operation on each element of the array.
Bitwise ORnumpy.bitwise_or(x1, x2)Performs a bitwise OR operation on each element of the array.
Bitwise XORnumpy.bitwise_xor(x1, x2)Performs a bitwise XOR operation on each element of the array.
Bitwise NOTnumpy.invert(x)Performs a bitwise NOT operation on each element of the array.
Left shiftnumpy.left_shift(x1, x2)Shifts each element of the array left by the specified number of bits.
Right shiftnumpy.right_shift(x1, x2)Shifts 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)

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:

  1. 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

  2. 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

  3. 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

  4. NOT operation (~):Invert each bit of the number, that is, 0 becomes 1 and 1 becomes 0.

    For example: ~1010 = 0101

  5. 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

  6. 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

import numpy as np print ('Binary form 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))

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

import numpy as np a,b = 13,17 print ('Binary form of 13 and 17:') print (bin(a), bin(b)) print ('Bitwise OR of 13 and 17:') print (np.bitwise_or(13, 17))

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:

  • will1(This is called: original code) Convert to binary =00000001
  • Bitwise inversion = 11111110
  • We find that the sign bit (i.e., the most significant bit) is1(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
  • ExpressionBinary value (two's complement)Decimal value
    5 00000000 00000000 00000000 000001015
    ~511111111 11111111 11111111 11111010 -6

Example

import numpy as np print ('Bitwise inversion of 13, where the dtype of the ndarray is uint8:') print (np.invert(np.array([13], dtype = np.uint8))) print ('\n') # Comparing the binary representations of 13 and 242, we can see the bit 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))

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

import numpy as np print ('Shift 10 left by two bits:') 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)) # The two bits in '00001010' moved to the left, and two 0s were added on the right.

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

import numpy as np print ('Shift 40 right by two bits:') 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)) # The two bits in '00001010' moved to the right, and two 0s were added on the left.

The output result is:

将 40 右移两位:
10


40 的二进制表示:
00101000


10 的二进制表示:
00001010
Other extensions