Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

NumPy Bitwise Operations

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

)

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 for calculation:

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

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

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

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

    For example: ~1010 = 0101

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

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

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

)

)

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

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

)

)

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:

Example

import

numpy

as

np

print

(

'

Bitwise inversion of 13, where the ndarray's dtype is uint8:

'

)

print

(

np

.

invert

(

np

.

array

(

[

13

]

,

dtype

=

np

.

uint8

)

)

)

print

(

'

\n

'

)

Comparing the binary representations of 13 and 242, we find the inversion of bits

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

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' 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

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' 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