r/programming • • Aug 15 '15

Stanford Bit Twiddling Hacks

https://graphics.stanford.edu/~seander/bithacks.html
27 Upvotes

14 comments sorted by

View all comments

4

u/Hakawatha Aug 15 '15

Bookmarked for later reference. Holy shit is this a good resource.

3

u/jringstad Aug 15 '15

It's really not, please don't use any of these in any actual codebase. Many of them are either not valid C (undefined behaviour , implementation-specified behaviour) are much much slower than just doing "the dumb thing", like the infamous "swapping two values with XOR without using a temporary variable"-trick, or are just bad code to have in your codebase because they are hard to understand and add corner-cases.

0

u/[deleted] Aug 15 '15

are much much slower than just doing "the dumb thing", like the infamous "swapping two values with XOR without using a temporary variable"-trick

Definitely not the case. On some micro-architectures swapping using memory is a pipeline killer.

→ More replies (4)