4 ms·
AKA "how to write line noise".
by bkudria 17y ago
AKA "how to write line noise".
- pkrumins 17y agoNot 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!
- bcl 17y agoAbsolutely! 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.
- pkrumins 17y agoExactly! And you can get these questions even in job interviews. If you want to score the intervieews, then you better know the quick ways!
- ramchip 17y agoI 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...
- swolchok 17y agoThe 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.
- kqr2 17y agoAlso, even if you don't use them, it's nice to be able to read through code and understand these "idioms".
- ajross 17y agoIf 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.
- bkudria 17y agoYou'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!
- GeneralMaximus 17y agoThis 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 http://base64.sourceforge.net/b64.c I guess I've just been spoiled by Python to not care :p