implementation of  Bit Twiddling Hacks at http://graphics.stanford.edu/~seander/bithacks.html