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

  1. Absolute value of a float
  2. Absolute value of an integer
  3. Approximative inverse of a float
  4. Bit manipulation basics
  5. Bit mask of lowest bit not set
  6. Count bits set in parallel a.k.a. Population Count
  7. Detects zero bytes inside a 32 bit integer
  8. Endianess
  9. Extend bit width
  10. Float inverse square root approximation
  11. Float square root approximation
  12. Is power of two
  13. Minimum / maximum of integers
  14. Parity
  15. Position of lowest bit set
  16. Round up to the next power of two
  17. Sign comparison
  18. Sign of a 32 bit integer
  19. Swap two values

Interactive

Javascript Bit Manipulator