NumPy by Ivan Idris

NumPy by Ivan Idris


Author:Ivan Idris
Language: eng
Format: epub
Publisher: Packt Publishing

What just happened?

We covered three bit twiddling hacks—checking whether the signs of integers are different, checking whether a number is a power of 2, and calculating the modulus of a number that is a power of 2. We saw the NumPy counterparts of the operators ^, &, <<, and < (see bittwidling.py):

from __future__ import print_function import numpy as np x = np.arange(-9, 9) y = -x print("Sign different?", (x ^ y) < 0) print("Sign different?", np.less(np.bitwise_xor(x, y), 0)) print("Power of 2?\n", x, "\n", (x & (x - 1)) == 0) print("Power of 2?\n", x, "\n", np.equal(np.bitwise_and(x, (x - 1)), 0)) print("Modulus 4\n", x, "\n", x & ((1 << 2) - 1)) print("Modulus 4\n", x, "\n", np.bitwise_and(x, np.left_shift(1, 2) - 1))


Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.