Live data from Hacker News

Bit Twiddling Hacks (2009)

graphics.stanford.edu

1–10 of 28 posts

Re: Bit Twiddling Hacks (2009)

#2
It's nice that it expresses algorithms in C. It's more concrete than most of Hacker's Delight, which is similar and more comprehensive, but often vaguer and occassionally covers tricks used on historical architectures and systems.[0]

0. Hacker's Delight (Second Edition) by Henry S. Warren, 2013, ISBN: 0321842685

Re: Bit Twiddling Hacks (2009)

#4

This thing is posted so often on HN. Does anyone have any new cool tricks that aren't in the normal compendium of tricks?

There are additional tricks that are more specialized or generalizations of the ideas here beyond their presented scope, like SWAR (SIMD Within A Register) over arbitrary packed bitfields. I realize this is not that helpful but most of the tricks not listed here won’t be interesting to most people. Many of the bit twiddling tricks that are widely known are also known to compilers, which can generate them automatically. Hell, x86 has single instructions that implement many of the well-known tricks directly.

Where these tricks become handy again is SIMD vectorization. The compiler is much less clever in these situations and the scalar bit-twiddling instructions often don’t have vectorized equivalents.

Re: Bit Twiddling Hacks (2009)

#5
with this bitwise magic, my code will find the sign faster than you can say "boolean operation!" who needs branching when you've got clever tricks up your sleeve? hate to say it, but this beats the obvious way, hands down!

Re: Bit Twiddling Hacks (2009)

#8
It's worth noting that modern x86 CPUs have specialized extensions for bit manipulation. Furthermore C23 added stdbit.h that has some operations for bit manipulation

https://en.wikipedia.org/wiki/X86_Bit_manipulation_instructi...

https://thephd.dev/_vendor/future_cxx/papers/C%20-%20Modern%...

Re: Bit Twiddling Hacks (2009)

#9
post #5

with this bitwise magic, my code will find the sign faster than you can say "boolean operation!" who needs branching when you've got clever tricks up your sleeve? hate to say it, but this beats the obvious way, hands down!

would modern compilers not convert the simple boolean operations into clever bit twiddling hacks for you?
Post reply on HN