News for hackers. Stuff that matters.
Re:Low Level Bit Hacks You Absolutely Must Know by bkudria on Tuesday June 30, 2009 @02:44PM (#681302)
AKA "how to write line noise".
Re:Low Level Bit Hacks You Absolutely Must Know by ajross on Tuesday June 30, 2009 @03:01PM (#681337)
If you read the article instead of sneering at it, you might find it less noisy. It's an idiom. All idioms involve some learning, and generally some odd, seemingly-misapplied abstractions. But they generally have value, too, which is why they persist. In this case, the bit tricks work to map a very common metaphor in the design domain (a set of boolean flags) to a very common low-level implementation choice (the machine word).

Does your application benefit from that kind of mapping? Probably not, if it's a web 2.0 thing. But don't pretend that this sort of thing is never useful either. Somewhere down in the layers you don't understand, there is real machine code running your computer. And that code, like yours, needs compact and simple storage of booleans.

Re:Low Level Bit Hacks You Absolutely Must Know by bkudria on Wednesday July 01, 2009 @03:29AM (#682240)
You're right - my comment was intentionally snarky, and deserved all the downvotes it got.

You're right - these are idioms. Idioms for working with current hardware. Essentially, these are machine implementation details, and while, yes, we currently have to program machines to get anything done, not everyone finds that kind of work rewarding or interesting.

I didn't mean to degrade the type of person that finds this interesting, I wanted to point out the divide between the hardware hacker and the, for a lack of a better term, math hacker.

I hope that clears up my intentions!

Re:Low Level Bit Hacks You Absolutely Must Know by pkrumins on Tuesday June 30, 2009 @02:51PM (#681316)
Not quite!

These are embedded programmer tools of trade. For example, you don't want to loop over bits to count number of bits in it (not covered in this article, but will be in the next part of the article). It can be done with several shifts and ANDs! Huge speedups!

Another example, finding the lowest order 1-bit in a 64bit integer - it can take up to 64 iterations to find it with a loop. Instead it takes one AND and one but inversion operation (= 2 ops total)! 32x speedup!

Re:Low Level Bit Hacks You Absolutely Must Know by swolchok on Wednesday July 01, 2009 @03:38PM (#683230)
The problem is that learning to think this way in general makes you habitually write things like x >> 8 instead of x / 256, which makes your code hard to read for no benefit if you're using a compiler that was written sometime in, oh, the past 20 years.

EDIT: sometimes x >> 8 is clearer, if you're actually dealing with packing bits into words, but I've just recently seen it done when the code really "meant" division.

Re:Low Level Bit Hacks You Absolutely Must Know by bcl on Tuesday June 30, 2009 @04:04PM (#681477)
Absolutely! Some of the examples, depending on the core architecture, could benefit from a table lookup, but all are good tools to have in the box.
Re:Low Level Bit Hacks You Absolutely Must Know by pkrumins on Tuesday June 30, 2009 @04:14PM (#681493)
Exactly! And you can get these questions even in job interviews. If you want to score the intervieews, then you better know the quick ways!
Re:Low Level Bit Hacks You Absolutely Must Know by ramchip on Wednesday July 01, 2009 @10:10AM (#682651)
I like bit twiddling, but I'm not sure I'd take the job if a primary concern of the interviewer is whether I know the quick way to find the lowest-order bit in a 64-bit int...
Re:Low Level Bit Hacks You Absolutely Must Know by kqr2 on Tuesday June 30, 2009 @02:52PM (#681317)
Also, even if you don't use them, it's nice to be able to read through code and understand these "idioms".
Re:Low Level Bit Hacks You Absolutely Must Know by GeneralMaximus on Wednesday July 01, 2009 @02:40AM (#682202)
This stuff is actually pretty useful.

I was recently looking at some C code that takes some text and converts it to Base64 encoding for sending it over an HTTP POST request. There was one function in my code that was, IIRC, about 30 lines long and completely unreadable. Later on, I found some code on SourceForge that did the same thing in 3 lines of bit shift operations.

Here is the code in question: http://base64.sourceforge.net/b64.c

I guess I've just been spoiled by Python to not care :p