What's This Web Site About ?Before saying anything else, let's declare the legal stuff: I collected all these little helpful pieces of code on the internet.
I, STEPHAN BRUMME, DIDN'T ORIGINALLY INVENT THEM.
However, the authors put them into the public domain, which is absolutely great.
Do whatever you want with the code, it's okay for commercial stuff, too.
Let's call it "tellware" - if you find it useful, tell all your colleagues about it.
By far the best web sites on the subject, and my inspiration, are The Aggregate from the University of Kentucky and "Bit Twiddling Hacks" by Sean Eron Anderson. Spend a minute or two and check out both !
Each bit twiddler trick is accompanied by full C code including a validation routine which proves correctness for all inputs. Nevertheless, if you find a bug, don't hesitate to contact me (see bottom of each page).
I give performance ratings for my own computer and I'm very keen on timings of different CPU architectures / compilers. Just download the sources and run them.
All The Twiddled Bits
- Absolute value of a float
- Absolute value of an integer
- Approximative inverse of a float
- Bit manipulation basics
- Bit mask of lowest bit not set
- Count bits set in parallel a.k.a. Population Count
- Detects zero bytes inside a 32 bit integer
- Extend bit width
- Float inverse square root approximation
- Float square root approximation
- Is power of two
- Minimum / maximum of integers
- Position of lowest bit set
- Round up to the next power of two
- Sign comparison
- Sign of a 32 bit integer
- Swap two values