Exact behavior of bitwise routines?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Exact behavior of bitwise routines?

This post has NOT been accepted by the mailing list yet.
I have a few questions about the exact behavior of the bitwise routines / binary operations listed on https://docs.scipy.org/doc/numpy/reference/routines.bitwise.html

1. The bitwise representation of signed numbers is implementation defined in C. It can be either two's complement, one's complement or sign-magnitude. Will Numpy bitwise operations use the native representation of the platform / compiler, or always operate on the two's complement representation of the numbers?

2. For a negative LHS, left shift is undefined behavior in C, and right shift is implementation defined: logical or arithmetic. From the Numpy documentation it would seem that both of these are defined in Numpy (as multiplication/division by 2**n followed by rounding towards -Inf). Can you confirm this?

3. For a negative RHS or a RHS that is larger than the width of the LHS, left and right shift are both undefined behavior in C. Trying this case in Numpy gives weird and type-dependent results (sometimes shift happens mod 32, sometimes mod 64, sometimes we get 0, sometimes we get answers with a new type, and sometimes we get TypeError). Is this undefined behavior in the nasal demons, C sense? Or will it just give an undefined answer or possibly raise? From the Numpy documentation it would seem that shifting by more than the width of the LHS should shift out all bits and thus result in 0, but doesn't seem to be how it works in practice.

Related discussion to point 3: