Bit Hacks

I found a short code from pyglet source code.


def _is_pow2(v):
    # http://graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2
    return (v & (v - 1)) == 0

At a glance, I can understand how it works, but I have never devised it!
It is from “Bit Hacks” by Sean Eron Anderson. They are not only beautiful but very useful in graphics libraries.

Leave a Reply