
Bit Twiddling Hacks (2005) - e-sushi
https://graphics.stanford.edu/~seander/bithacks.html
======
byuu
Just a fair warning as someone who has made extensive use of this page in the
past: many of these hacks often end up slower these days on modern CPUs than
naive approaches. Especially the no-branch and no-multiply math ops.

As always, be sure to performance test your code if you're getting into
obfuscating your algorithms to boost speeds.

That said, there are still plenty of real gems in there. And on that note,
here's one of my favorite bit-twiddling hacks:

[http://webcache.googleusercontent.com/search?q=cache:www.sla...](http://webcache.googleusercontent.com/search?q=cache:www.slack.net/~ant/info/rgb_clamped_add.html)

[http://webcache.googleusercontent.com/search?q=cache:www.sla...](http://webcache.googleusercontent.com/search?q=cache:www.slack.net/~ant/info/rgb_clamped_sub.html)

[http://webcache.googleusercontent.com/search?q=cache:www.sla...](http://webcache.googleusercontent.com/search?q=cache:www.slack.net/~ant/info/rgb_mixing.html)

(these functions are for RGB555, but you can easily extend these to RGB888
ops.)

~~~
repsilat
One place the branchless techniques can come in handy is with SIMD (or
simulated SIMD by packing several small ints into longer ones.) A branchless
algorithm that's 50% slower can pay for itself if you can process four
elements at a time.

Agreed that performance testing is absolutely necessary, preferrably with
something that can tell you not only where exactly the processor is spending
its time but also whether it's spending its time computing or waiting on
memory etc, whether branches are being predicted well...

------
lifthrasiir
I would suggest Hacker's Delight too; the Chapter 2, Basics, is freely
available at the website [1]. Pretty sure that the linked algorithm is also
there (don't have a copy right now so can't check).

[1] [http://hackersdelight.org/](http://hackersdelight.org/)

~~~
zimmerfrei
These hacks have actually been modeled and generalized into a mathematical
construct called Mixed Boolean Arithmetic [1][2]. With a few matrix
operations, you can actually generate an arbitrary number of hacky-looking (or
obfuscated, as that was the actual intention of the authors of [1])
expressions from any initial, simple expression.

[1]
[http://link.springer.com/chapter/10.1007/978-3-540-77535-5_5](http://link.springer.com/chapter/10.1007/978-3-540-77535-5_5)
(and I deeply apologize I can't find the free PDF version, though sci-hub may
help here)

[2] [http://blog.quarkslab.com/what-theoretical-tools-are-
needed-...](http://blog.quarkslab.com/what-theoretical-tools-are-needed-to-
simplify-mba-expressions.html)

~~~
nick_name
>> and I deeply apologize I can't find the free PDF version, though sci-hub
may help here)

[https://sci-hub.cc/10.1007/978-3-540-77535-5_5](https://sci-
hub.cc/10.1007/978-3-540-77535-5_5)

~~~
0xmohit
Use [https://sci-hub.ac/10.1007/978-3-540-77535-5_5](https://sci-
hub.ac/10.1007/978-3-540-77535-5_5) in order to prevent browsers complaining
about certificate mismatch.

------
esneider
I wrote that algorithm! A year later I found Hacker's delight, a wonderful
book that had a much "faster" version of the same algorithm (less ops).

For anyone interested in this kind of things, you should go read that book. It
actually is a delight.

~~~
vowelless
Which algorithm are you referring to?

~~~
raimue
I'd guess the linked one at the anchor #NextBitPermutation.

~~~
WaxProlix
Anchor has since been removed, so the gp comment will look odd from here on
out.

------
jhallenworld
>[https://graphics.stanford.edu/~seander/bithacks.html#Integer...](https://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax)

The quick and dirty version is still useful (portable, works for all values)
for the case of 1/2 word size unsigned versions of min and max:

    
    
        unsigned max(unsigned short a, unsigned short b)
        {
            unsigned q = a - b;
            unsigned r = ((~q) >> 16);
            return r + b;
        }

~~~
yitchelle
Does the right shift limits this to certain word sizes?

------
vowelless
Site seems down. Here is the google cache:

[https://webcache.googleusercontent.com/search?q=cache:__L7kb...](https://webcache.googleusercontent.com/search?q=cache:__L7kblMgDMJ:https://graphics.stanford.edu/~seander/bithacks.html+&cd=1&hl=en&ct=clnk&gl=ca)

------
mungoman2
There are some neat tricks there about interleaving bits from two operands.
But what about from 3?

~~~
Kristine1975
Maybe this helps: [http://www.forceflow.be/2013/10/07/morton-
encodingdecoding-t...](http://www.forceflow.be/2013/10/07/morton-
encodingdecoding-through-bit-interleaving-implementations/)

------
kamijo-k
The author's name seems to be recursive...

------
tedmiston
@dang - Can we remove the fragment from this URL?

Current:
[https://graphics.stanford.edu/~seander/bithacks.html#NextBit...](https://graphics.stanford.edu/~seander/bithacks.html#NextBitPermutation)

Proposed:
[https://graphics.stanford.edu/~seander/bithacks.html](https://graphics.stanford.edu/~seander/bithacks.html)

~~~
sctb
Thanks, we've updated the link.

