pythonnumpyvectorizationxor

XORing all values of a (2d) numpy array together


If I have a 2d numpy array like (the data is not necessarily sequential but for example)

[[0. 1. 2. 3.]
 [4. 5. 6. 7.]
 [8. 9. 10. 11.]]

how can I compute 0^1^2^3^4^5^6^7^8^9^10?

I think I could flatten it and then use np.vectorize() but I was wondering if it was possible with numpy operations directly? From here I tried np.bitwise_xor.reduce(a) but it says TypeError: No loop matching the specified signature and casting was found for ufunc bitwise_xor


Solution

  • You need to convert your arrays to some integer type

    np.bitwise_xor.reduce(np.array([0.0, 1.0, 2.0, 3.0]).astype(np.int32))
    

    -> 0