NumPy Binary operations: left_shift() function
numpy.left_shift() function
The left_shift() function is used to shift the bits of an integer to the left.
Some common applications of numpy.left_shift() include: Multiplication:
Performing fast multiplication of integers by powers of two, as left-shifting a binary number by 'n' positions is equivalent to multiplying the number by 2^n.
Data Manipulation: Changing the position of bits in data, such as packing or unpacking data structures, combining or splitting fields in data records, or adjusting bitmasks.
Cryptography: Applying bitwise left shift operations during encryption or decryption processes, especially in algorithms that involve bit manipulation or key generation.
Error Detection and Correction: Implementing error detection and correction algorithms that involve left shift operations on binary data.
Syntax:
numpy.left_shift(x1, x2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj ]) = <ufunc 'left_shift'>
Parameters:
Name | Description | Required / Optional |
---|---|---|
x1 | Input values. | Required |
x2 | Number of zeros to append to x1. Has to be non-negative | Required |
out | A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. | Optional |
where | Values of True indicate to calculate the ufunc at that position, values of False indicate to leave the value in the output alone. | Optional |
**kwargs | For other keyword-only arguments. |
Return value:
out: [array of integer type]
Return x1 with bits shifted x2 times to the left. This is a scalar if both x1 and x2 are scalars.
Example: Bitwise NOT and left shift operations with NumPy and binary representation
>>> import numpy as np
>>> np.invert(np.array([True, False]))
array([False, True])
>>> np.binary_repr(8)
'1000'
>>> np.left_shift(8,2)
32
>>> np.binary_repr(32)
'100000'
In the above code - first, the numpy.invert() function is applied to a NumPy array containing boolean values ([True, False]). The resulting array contains the inverted boolean values ([False, True]).
Next, the numpy.binary_repr() function is used to display the binary representation of the number 8, which is '1000'. The numpy.left_shift() function is then used to perform a bitwise left shift operation on the number 8, shifting it two positions to the left. The result of the left shift operation is 32. The binary representation of the resulting number (32) is displayed using numpy.binary_repr(), which is '100000'.
Example: Bitwise left shift operation with NumPy and varying shift amounts
>>> import numpy as np
>>> np.left_shift(8,[1,2,3])
array([16, 32, 64], dtype=int32)
In the above example, the integer 8 is left-shifted by the values in the array [1, 2, 3], resulting in a new array with the shifted values ([16, 32, 64]). Each element in the output array is the result of the left shift operation applied to the number 8 with the corresponding shift amount from the input array.
Python Code Editor:
Previous: invert()
Next: right_shift()
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/numpy/binary-operations/left-shift.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics