Mike made a comment about his favorites bit twiddling methods, taken from Bit Twiddling Hacks by Sean Eron Anderson. Worthy of any programmers time to read.
I wanted to add that there is a book written about the subject as well, Hacker's Delight by Henry S. Warren Jr; Addison-Wesley, 2003. ISBN: ISBN-13: 978-0201914658 Along with the books adjunct site.
The book covers the fast way to get many of the common firmware problems solved. It is one of the books within easy reach of my development system most of the time.
It would be interesting to see if using the modern 'Typeof' found in newer compilers would let us do any of these bit twiddles even better.
Now for the down side. If I was doing a Code Inspection and came across a magic number like 0x077CB531UL, with no explanation that this is a de Bruijn sequence, in the comments without lots of references, I would be appalled.
Bit Twiddling hacks are great in the few times that you need them, where you have to minimize branches, or maximize performance in a limited area, but always keep in mind the best programmers write simple code...
Post a Comment