If you're interested in reading about more of these, and how they work I'd highly suggest reading the book Hacker's Delight by Henry S. Warren[1]. I know that I've used more than a few of the tricks in my day-to-day work. [1] http://www.amazon.com/Hackers-Delight-Henry-S-Warren/dp/0201...
Low Level Bit Hacks You Must Know
41–49 of 49 posts
Re: Low Level Bit Hacks You Must Know
#42Other's have already mentioned but for real hacks in the unintuitive sense, check out Hacker's Delight and http://graphics.stanford.edu/~seander/bithacks.html.
Re: Low Level Bit Hacks You Must Know
#43Here's a much more comprehensive collection of bit hacks: http://graphics.stanford.edu/~seander/bithacks.html
It depresses me that something as simple as int mask = v >> sizeof(int) * CHAR_BIT - 1; unsigned int r = (v ^ mask) - mask; can be granted a patent ( http://graphics.stanford.edu/~seander/bithacks.html#IntegerA... ).
Re: Low Level Bit Hacks You Must Know
#44Bit Hack #6. Turn off the rightmost 1-bit. Now it finally gets more interesting!!! Bit hacks #1 - #5 were kind of boring to be honest. Does anybody know a practical use case for that? I have personally never encountered a situation were I needed to manipulate the right most 1-bit. Otherwise it's a nice introduction to bit hacking.
while flags!=0:
right_most = flags & (-flags)
process(right_most)
flags &= ~right_mostRe: Low Level Bit Hacks You Must Know
#45Earlier quoted context omitted.
Ha. Next time someone asks how to swap two values at an interview question (I think this is pretty common): #define SWAP(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))
Is it pretty common? As someone who does a fair number of software engineer interviews, that's a trick question. The real answer to "swap two vars with no temps" is: 1. Don't be clever in our code base. Use a temp variable. 2. There's various dumb tricks with XOR, and possibly add/subtract if overflows don't break. 3. A sequence of several instructions where each of them requires the result of the previous one may no…
Re: Low Level Bit Hacks You Must Know
#46The intersection of tricks on all those sites is quite large but on every one of them there's something you haven't seen before.
By the way if you like to wrap your mind around such tricks you might also find some gems here: http://www.azillionmonkeys.com/qed/asmexample.html
Re: Low Level Bit Hacks You Must Know
#47Here's a much more comprehensive collection of bit hacks: http://graphics.stanford.edu/~seander/bithacks.html
Ha. Next time someone asks how to swap two values at an interview question (I think this is pretty common): #define SWAP(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))
The very best candidates add: Because it's tricky, hard to read, limited in scope, and usually you can avoid swapping variables by changing their usage downstream. Besides, the best compilers will sort it out for you if you write it clearly and cleanly.
When I interview it's not the answers I listen to, it's the knowledge they expose, not of programming per se, but of good practices in programming.
Re: Low Level Bit Hacks You Must Know
#48Earlier quoted context omitted.
Embedded and firmware engineering questions expect this as an answer. Anyone following your advice will not be taken seriously. This is true regardless of whether you're correct factually. Readers of this thread deserve to know that.
3 is actually a quite valid point for embedded. Any swap actually will be expensive when it comes to keeping cache lines clean. The correct answer in that case is just to not swap the variables, and instead swap their uses later on: int x, y; ... SWAP(x, y); foo(x, y); becomes int x, y; ... foo(y, x); (naturally, this is why I still eagerly await the arrival of a C compiler that has macros with LISP power)
http://chaos-pp.cvs.sourceforge.net/chaos-pp/order-pp/exampl...
But I do not envy the poor soul who would have to maintain all this cleverness.