numpy.left_shift with negative x2

Hello, the following operation seems strange to me
np.left_shift(2,-1) 0
I would have expected a right_shift by one. The documentation on http://docs.scipy.org/doc/numpy/reference/generated/numpy.left_shift.html#nu... also says that the operation is equivalent to multiplying x1 by 2**x2. That's not the case! Markus

On Wed, Feb 3, 2010 at 8:43 PM, <markus.proeller@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.
I wouldn't expect anything, the behavior is simply not defined. Python returns an error: In [17]: 2 << -1 --------------------------------------------------------------------------- ValueError ....
The documentation on
http://docs.scipy.org/doc/numpy/reference/generated/numpy.left_shift.html#nu... also says that the operation is equivalent to multiplying x1 by 2**x2. That's not the case!
The line before that says "Bits are shifted to the left by appending `x2` 0s at the right of `x1`." What does it mean to append a negative number of zeros? The docs could explicitly mention that x2 has to be non-negative, if that would make it clearer. Cheers, Ralf

On Wed, Feb 3, 2010 at 8:43 PM, <markus.proeller@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.
I wouldn't expect anything, the behavior is simply not defined.
But it would prevent a statement like if x2 > 0 then ... else ... Markus

On Wed, Feb 3, 2010 at 9:33 PM, <markus.proeller@ifm.com> wrote:
On Wed, Feb 3, 2010 at 8:43 PM, <markus.proeller@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.
I wouldn't expect anything, the behavior is simply not defined.
But it would prevent a statement like
if x2 > 0 then ... else ...
Right now I think the left_shift ufunc calls the Python C API, so it just
does the same as Python. Which seems like the right thing to do. If you want a bit_shift function which is a combination of left and right shift, this is straightforward to do right? Something like: In [42]: x2 Out[42]: array([-2, -1, 0, 1, 2]) In [43]: def bit_shift(x1, x2): return np.choose(x2>0, [np.right_shift(x1, -x2), np.left_shift(x1, x2)]) ....: In [45]: bit_shift(2, x2) Out[45]: array([0, 1, 2, 4, 8]) Cheers, Ralf

On Wed, Feb 3, 2010 at 5:43 AM, <markus.proeller@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
participants (3)
-
Charles R Harris
-
markus.proeller@ifm.com
-
Ralf Gommers