Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy bitwise operation error

Tags:

python

numpy

numpy version 1.9.0

1 & (2**63)
0

np.bitwise_and(1, 2**63)
TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

np.bitwise_and(1, 2**63 + 100)
TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

np.bitwise_and(1, 2**64)
0

Is this a bug or am I missing something?

like image 480
jf328 Avatar asked Dec 14 '25 06:12

jf328


1 Answers

convert to uint64 first:

np.bitwise_and(np.uint64(1), np.uint64(2**63))

Here is the code to check the rule to convert python integer to numpy integer:

print np.array([2**30]).dtype
print np.array([2**31]).dtype
print np.array([2**63]).dtype
print np.array([2**64]).dtype

output:

int32
int64
uint64
object

I think np.bitwise_and(1, 2**63) raise error because 2**63 is out of the range of int32 and int64.

np.bitwise_and(1, 2**64) works because it will use Python's long object.

We need to read the source code to understand the detail.

like image 144
HYRY Avatar answered Dec 16 '25 19:12

HYRY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!