YouTip LogoYouTip

Numpy Binary Operators

NumPy Bitwise Operations

Bitwise operations are a class of operations that work at the bit level of binary numbers. They directly manipulate the individual bits of binary numbers without considering the overall value of the number.

NumPy provides a series of bitwise operation functions that allow element-wise operations on arrays. These operations are similar to Python's bitwise operators but work on NumPy arrays, supporting vectorized processing for higher performance.

Bitwise operations are widely used in computer science for optimization and processing low-level data.

NumPy 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) Compute the bitwise AND of two arrays element-wise.
Bitwise OR numpy.bitwise_or(x1, x2) Compute the bitwise OR of two arrays element-wise.
Bitwise XOR numpy.bitwise_xor(x1, x2) Compute the bitwise XOR of two arrays element-wise.
Bitwise NOT numpy.invert(x) Compute the bitwise NOT (inversion) of an array element-wise.
Left Shift numpy.left_shift(x1, x2) Shift the bits of an array element-wise to the left by the specified number of positions.
Right Shift numpy.right_shift(x1, x2) Shift the bits of an array element-wise to the right by the specified number of positions.

Examples

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 Inversion

 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 operators like "&", "~", "|", and "^" for calculations:

  1. AND Operation (&): The result is 1 if both bits at the corresponding positions are 1; otherwise, the result is 0.
  2. Example: 1010 & 1100 = 1000

  3. OR Operation (|): The result is 1 if at least one of the bits at the corresponding positions is 1; otherwise, the result is 0.
  4. Example: 1010 | 1100 = 1110

  5. XOR Operation (^): The result is 1 if the bits at the corresponding positions are different; if they are the same, the result is 0.
  6. Example: 1010 ^ 1100 = 0110

  7. NOT Operation (~): Inverts each bit of the number, i.e., 0 becomes 1, and 1 becomes 0.
  8. Example: ~1010 = 0101

  9. Left Shift Operation (<<): Shifts all bits of the number to the left by the specified number of positions, filling the right side with 0s.
  10. Example: 1010 << 2 = 101000

  11. Right Shift Operation (>>): Shifts all bits of the number to the right by the specified number of positions, filling the left side based on the sign bit or with 0s.
  12. Example: 1010 >> 2 = 0010

bitwise_and

The bitwise_and() function performs a bitwise AND operation on the binary representation 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))

The output is:

Binary forms of 13 and 17:0b1101 0b10001Bitwise 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 for 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 representation 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))

The output is:

Binary forms of 13 and 17:0b1101 0b10001Bitwise 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 for 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 integers in an array, i.e., 0 becomes 1, and 1 becomes 0.

For signed integers, it takes the two's complement of the binary number and then adds 1. In binary numbers, the most significant bit (MSB) being 0 indicates a positive number, and being 1 indicates a negative number.

Let's look at the calculation steps for ~1:

  • Convert 1 (here called: original code) to binary = 00000001
  • Invert each bit = 11111110
  • Notice the sign bit (i.e., the most significant bit) is 1 (indicating a negative number), invert all bits except the sign bit = 10000001
  • Add 1 to the least significant bit to get its two's 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

import numpy as np print('Bitwise inversion of 13, where the dtype of the ndarray is uint8:')print(np.invert(np.array(, dtype = np.uint8)))print('n')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:

Bitwise inversion of 13, where the dtype of the ndarray is uint8:Binary representation of 13:00001101Binary representation of 242:11110010

left_shift

The left_shift() function shifts the binary representation of array elements to the left by the specified number of positions, appending an equal number of 0s on the right.

Example

import numpy as np print('Left shift 10 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))

The output is:

Left shift 10 by two positions:40Binary representation of 10:00001010Binary representation of 40:00101000

right_shift

The right_shift() function shifts the binary representation of array elements to the right by the specified number of positions, appending an equal number of 0s on the left.

Example

import numpy as np print('Right shift 40 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))

The output is:

Right shift 40 by two positions:10Binary representation of 40:00101000Binary representation of 10:00001010
← Numpy Statistical FunctionsNumpy Advanced Indexing β†’