8000 Inconsistent bitwise shift overflow behavior · Issue #10299 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content
Inconsistent bitwise shift overflow behavior #10299
Closed
@toobaz

Description

@toobaz
In [2]: np.arange(20, dtype='uint') << 64
Out[2]: array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=uint64)

In [3]: np.arange(10, dtype='uint') << 64
Out[3]: array([0, 0, 0, 0, 0, 0, 0, 0, 8, 9], dtype=uint64)

In [4]: np.arange(5, dtype='uint') << 64
Out[4]: array([0, 1, 2, 3, 4], dtype=uint64)

My understanding is that while numpy.left_shift should execute a rotation (despite the docs not mentioning this word), the << operator should execute a shift (that is, drop overflowing bits). At least, things seem to work this way with values below 64:

In [5]: (np.arange(20, dtype='uint') << 63 == (np.arange(20, dtype='uint') % 2 * 2) ** 63).all()
Out[5]: True

In [6]: (np.arange(10, dtype='uint') << 63 == (np.arange(10, dtype='uint') % 2 * 2) ** 63).all()
Out[6]: True

In [7]: (np.arange(5, dtype='uint') << 63 == (np.arange(5, dtype='uint') % 2 * 2) ** 63).all()
Out[7]: True

Instead with 64 some cases (In [2]:) shift, some (In [4]:) rotate, and some (In [3]:) mix the two behaviors!

I'm using uint for simplicity, but the behavior is not limited to unsigned ints:

In [8]: np.arange(20) << 64
Out[8]: array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

In [9]: np.arange(5) << 64
Out[9]: array([0, 1, 2, 3, 4])

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0