Home > Blockchain >  Preserve scalar dtype when bit shifting
Preserve scalar dtype when bit shifting

Time:10-27

I have a 32 bit uint scalar, how do i bit shift it without upcasting the dtype?

x = np.uint32(123456789)
x << 11 

int64 Output:

252839503872

Expected output:

3731400704

It is possible to get my desired output by doing np.uint32((x << 11) & 0xFFFFFFFF), but the syntax feels superfluous for such an easy operation.

CodePudding user response:

Both arguments have to have the desired dtype:

In [80]: np.left_shift(x,np.uint32(11))
Out[80]: 3731400704
In [81]: x<<np.uint32(11)
Out[81]: 3731400704

x<<11 is an easy operation, but the dtype of the np.array(11) has control.

CodePudding user response:

You can use np.uint32(x << 11) it'll will produce the expected output

  • Related