Binary Operations
Master NumPy binary operations for efficient element-wise calculations in machine learning. Explore addition, subtraction, bitwise logic & shifting for data preprocessing.
Binary Operations in NumPy
NumPy's binary operators enable element-wise computations between two operands, most commonly arrays of the same shape. These operations facilitate a wide range of mathematical and logical calculations, including addition, subtraction, bitwise logic, and shifting.
Binary operations are fundamental to array manipulation, proving essential in data processing, machine learning preprocessing, and numerical simulations. Typically, the result of a binary operation is a new array where each element is the outcome of applying the operation to the corresponding elements of the input arrays.
Common Bitwise Functions in NumPy
NumPy offers a rich set of functions for performing binary and bitwise operations. The table below summarizes key functions:
| Operation | Description | | :----------------- | :--------------------------------------------------- | | bitwise_and
| Computes the bitwise AND of elements. | | bitwise_or
| Computes the bitwise OR of elements. | | bitwise_xor
| Computes the bitwise XOR of elements. | | left_shift
| Shifts bits to the left. | | right_shift
| Shifts bits to the right. | | bitwise_right_shift
| Shifts bits to the right (alias for right_shift
). | | invert
| Computes bitwise NOT. | | bitwise_invert
| Inverts each bit (alias for invert
). | | packbits
| Packs binary values into bytes. | | unpackbits
| Unpacks bytes into individual bits. | | binary_repr
| Returns the binary string representation of an integer. |
Detailed Explanation of Bitwise Operations
1. Bitwise AND (&
)
The bitwise AND operation performs a logical AND operation on each pair of corresponding bits. The result is 1 only if both bits are 1; otherwise, it is 0.
Example:
import numpy as np
a = np.array([5]) # Binary: 0101
b = np.array([3]) # Binary: 0011
result = np.bitwise_and(a, b) # or a & b
print(f"The result of bitwise AND between {a} and {b} is: {result}")
Output:
The result of bitwise AND between [5] and [3] is: [1]
Explanation:
0101 (5)
& 0011 (3)
------
0001 (1)
2. Bitwise OR (|
)
The bitwise OR operation returns 1 if either or both corresponding bits are 1; otherwise, it returns 0.
Example:
import numpy as np
a = np.array([5]) # Binary: 0101
b = np.array([3]) # Binary: 0011
result = np.bitwise_or(a, b) # or a | b
print(f"The result of bitwise OR between {a} and {b} is: {result}")
Output:
The result of bitwise OR between [5] and [3] is: [7]
Explanation:
0101 (5)
| 0011 (3)
------
0111 (7)
3. Bitwise NOT (~
)
The bitwise NOT operation inverts each bit of the operand. For signed integers, this is typically performed using two's complement representation.
Example:
import numpy as np
a = np.array([5]) # Binary: 0101 (assuming a certain bit width)
result = np.invert(a) # or ~a
print(f"The result of bitwise NOT on {a} is: {result}")
Output:
The result of bitwise NOT on [5] is: [-6]
Explanation: The exact binary representation and the resulting decimal value depend on the integer's bit width and signedness. For a standard 32-bit signed integer, ~5
results in -6
.
4. Left Shift (<<
)
The left shift operation shifts the bits of an operand to the left by a specified number of positions. The vacant positions on the right are filled with zeros. This operation is equivalent to multiplying the number by $2^n$, where $n$ is the number of positions shifted.
Example:
import numpy as np
a = np.array([5]) # Binary: 0101
result = np.left_shift(a, 1) # or a << 1
print(f"The result of left shifting {a} by 1 is: {result}")
Output:
The result of left shifting [5] by 1 is: [10]
Explanation:
0101 (5) << 1 => 1010 (10)
5. Right Shift (>>
)
The right shift operation shifts the bits of an operand to the right by a specified number of positions. The rightmost bits are discarded. For signed integers, the vacant positions on the left are filled with the sign bit (arithmetic shift), preserving the sign of the number. This operation is equivalent to integer division by $2^n$.
Example:
import numpy as np
a = np.array([5]) # Binary: 0101
result = np.right_shift(a, 1) # or a >> 1
print(f"The result of right shifting {a} by 1 is: {result}")
Output:
The result of right shifting [5] by 1 is: [2]
Explanation:
0101 (5) >> 1 => 0010 (2)
SEO Keywords: Binary operations in NumPy, NumPy bitwise operators, NumPy binary AND OR NOT, Python bitwise shift, NumPy binary shift, Left shift and right shift in NumPy, NumPy binary manipulation, NumPy tutorial for beginners, NumPy bitwise example, NumPy logical binary operations.