3

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?

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It works. Do you think this is a bug or is there a reason that a manual convert is needed here? Note that 2**64 doesn't have any problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.