Low Level Bit Hacks You Absolutely Must Know

For those software folks who are looking deeper into embedded systems development:

Enjoy,

Mike

Check out Hacker’s Delight for even more (crazy) bit hacking algorithms.

–Ryan

Thanks for sharing,

An embedded programming refresher is always useful especially with the growth of graphical programs that abstract away bit level details.

Since they are based on discrete and Boolean mathematics, bit hacks are actually equally valid for LV and any language with rich numerical types and operators. It may not be obvious, but the Boolean operators in LV work on all integer types, and make for concise implementation of many of them.

If you like the general bit hacks and truly want to remember and use them well, I encourage you to try your hand at proofs for them. They don’t have to be fully rigorous, but that is of course a useful thing to practice too.

Greg McKaskle

If any of my students use any of these bit hacks without commenting them, I will smack them.

The first 3 deserve maybe only a noogie, don’t you think?

**

Any programmer who doesn’t comment his/her work, no matter the project, deserves to be deprived of caffeine for a week or more. If that doesn’t do the trick, proceed to the harsher methods… like noogies and smacks with a mouse cord…

Isn’t caffeine depravation pretty much the harshest method available? Programming is one application where “shoot first, ask questions later” may not apply.

But altering register values via bitwise operations in nested ternary commands is so much fun! :rolleyes:

Smack!

artdura04, you’re amazing. The only thing I can think to add to that is that it has to be done inside an obfuscated macro using a templated function pointer and several magic numbers.

or you can be a super-meanie and use a classic turing-style method for making a hello world program, like I did:

#include <stdio.h>
int main(){char* p=new char[1024];++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;while(*p){++p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++p;++*p;++*p;++*p;++p;++*p;--p;--p;--p;--p;--*p;}++p;++*p;++*p;putchar(*p);++p;++*p;putchar(*p);++*p;++*p;++*p;++*p;++*p;++*p;++*p;putchar(*p);putchar(*p);++*p;++*p;++*p;putchar(*p);++p;++*p;++*p;putchar(*p);--p;--p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;putchar(*p);++p;putchar(*p);++*p;++*p;++*p;putchar(*p);--*p;--*p;--*p;--*p;--*p;--*p;putchar(*p);--*p;--*p;--*p;--*p;--*p;--*p;--*p;--*p;putchar(*p);++p;++*p;putchar(*p);++p;putchar(*p);return 0;}

its evil