w3resource

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.
Note: Bits are shifted to the left by appending x2 0s at the right of x1. Since the internal representation of numbers is in binary format, this operation is equivalent to multiplying x1 by 2**x2

Version: 1.15.0

Syntax:

numpy.left_shift(x1, x2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None,
subok=True[, signature, extobj ]) = <ufunc 'left_shift'>

Parameter:

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-1: numpy.left_shift() function

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

Example-2: numpy.left_shift() function

>>> import numpy as np
>>> np.left_shift(8,[1,2,3])
array([16, 32, 64], dtype=int32)

Python Code Editor:

Previous: invert()
Next: right_shift()