|C code||Description||Obtained from|
|Swap a and b around||ComputerShopper|
|return first bit set||Eivind Eklund|
|return first unset bit||Eivind Eklund|
|i will contain the number of bits set in c||Eivind Eklund|
|m will now contain number of bits set in the original m||popcnt in FreeBSD's sys/i386/i386/mp_machdep.c|
|v will now contain number of bits set in the original v||popcnt comment in FreeBSD's sys/i386/i386/mp_machdep.c|
|macro to return number of bits in x (assumes 32bit integer)||BSD fortune|
|reverse bits in a 32bit integer||BSD fortune|
|popcnt(x ^ (x - 1)) & 31||return first bit set in x, where popcnt returns number of bits set||Peter Wemm|
|ffs using a lookup table||return first bit set||Colin Percival|
If you have any other cool bitwise tricks you know about, please e-mail them to John-Mark Gurney <email@example.com>, and I will add them to the list.
A large collection of them can be found at Bit Twiddling Hacks.
Copyright © 1999-2003 by John-Mark Gurney.
All Rights Reserved.