Skip to content

BUG: Bitshift operations on scalar integers change type to int64 or fail #21261

Open
@maxnoe

Description

@maxnoe

Describe the issue:

Scalar bitshifts seem not to be implemented and are either upcasting to int64 or failing to upcast (for uint64).

Reproduce the code example:

import numpy as np


dtypes = np.uint16, np.uint32, np.uint64


for dtype in dtypes:
    scalar = dtype(15)
    array = np.array([15], dtype=dtype)

    print("dtype = ", dtype)

    print("Scalar right shift: ", end="")
    try:
        result = scalar >> 1
        print(result, type(result))
    except:
        print("Error")


    print("Scalar left shift: ", end="")
    try:
        result = scalar << 1
        print(result, type(result))
    except:
        print("Error")


    print("Array right shift: ", end="")
    try:
        result = array >> 1
        print(result, result.dtype)
    except:
        print("Error")

    print("Array left shift: ", end="")
    try:
        result = array << 1
        print(result, result.dtype)
    except:
        print("Error")

    print()

Error message:

❯ python bitshift_dtypes.py
dtype =  <class 'numpy.uint16'>
Scalar right shift: 7 <class 'numpy.int64'>
Scalar left shift: 30 <class 'numpy.int64'>
Array right shift: [7] uint16
Array left shift: [30] uint16

dtype =  <class 'numpy.uint32'>
Scalar right shift: 7 <class 'numpy.int64'>
Scalar left shift: 30 <class 'numpy.int64'>
Array right shift: [7] uint32
Array left shift: [30] uint32

dtype =  <class 'numpy.uint64'>
Scalar right shift: Error
Scalar left shift: Error
Array right shift: [7] uint64
Array left shift: [30] uint64
``

NumPy/Python version information:

❯ python -c 'import sys, numpy; print(numpy.version, sys.version)'
1.21.5 3.10.2 (main, Jan 15 2022, 19:56:27) [GCC 11.1.0]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions