Live data from Hacker News

Low Level Bit Hacks You Must Know

catonmat.net

41–49 of 49 posts

Re: Low Level Bit Hacks You Must Know

#41

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...

Indeed, a great book. It's also mentioned in the article.

Re: Low Level Bit Hacks You Must Know

#42
These aren't hacks really: they're just basic operations in binary arithmetic that anyone who has seriously coded C or assembly must have bumped into, probably quite early.

Other'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

#43

Here'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... ).

Granted, yes. There was discussion about whether the patent is at all valid, given abundant prior art. However, it depresses me too because you probably need lots of money and possibly a lawsuit to settle out the validity issue.

Re: Low Level Bit Hacks You Must Know

#44
post #2

Bit 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.

If you're handed a bit mask of events or flags then it can be used to iterate through the bits that are set, rather than all the bits.

    while flags!=0:
        right_most = flags & (-flags)
        process(right_most)
        flags &= ~right_most

Re: Low Level Bit Hacks You Must Know

#45

Earlier 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…

The question I've heard is to just swap 2 variables, no restrictions.

Re: Low Level Bit Hacks You Must Know

#46
Here are some more bit hack algorithms http://www.aggregate.org/MAGIC/

The 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

#47

Here'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)))

As an interviewer, the real value in this question is to get the attitude of the person behind it. The best candidates know it because they've read widely and know the tricks. They also then add "But I wouldn't use it."

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

#48
post #37
post #33

Earlier 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)

It's already there:

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.

Post reply on HN