[Numpy-discussion] numpy.left_shift with negative x2

Charles R Harris charlesr.harris at gmail.com
Wed Feb 3 12:09:37 EST 2010


On Wed, Feb 3, 2010 at 5:43 AM, <markus.proeller at ifm.com> wrote:

>
> Hello,
>
> the following operation seems strange to me
>
> >>> np.left_shift(2,-1)
> 0
>
> I would have expected a right_shift by one.
>
>
The result of a shift by a negative number is undefined in the C language;
the gcc compiler will issue a warning if it can determine that that is the
case. Even so, the result in your example is normally 1. There is something
else going on:

In [26]: x = array([2])

In [27]: x << -2
Out[27]: array([-9223372036854775808])

In [28]: x << 62
Out[28]: array([-9223372036854775808])

In [29]: x << 63
Out[29]: array([0])

In [30]: x << 64
Out[30]: array([2])

This for 64 bit integers. Looks almost like the shift is taken mod 64, which
is a bit weird.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20100203/8fbb7d9a/attachment.html>


More information about the NumPy-Discussion mailing list