6 ms·
The big constants used in the formula void to_bin(unsigned char c, char *out) { *(unsigned long long*)out = 3472328296227680304ULL + (((c * 9
by acqq 11y ago
The big constants used in the formula
void to_bin(unsigned char c, char *out) {
*(unsigned long long*)out = 3472328296227680304ULL +
(((c * 9241421688590303745ULL) / 128) & 72340172838076673ULL);
}
are much more obvious when they remain in hex:
0x3030303030303030 -- the byte array of '0's encoded as ASCII
0x8040201008040201 -- the magic
0x101010101010101 -- only 0 or 1 to be added to the appropriate array element
Therefore, more readable:
void to_bin(unsigned char c, char *out) {
/* endian dependent, works on x86 */
*(unsigned long long*)out = 0x3030303030303030ULL +
(((c * 0x8040201008040201ULL) >> 7) & 0x0101010101010101ULL);
}
The function is of course endian dependent (I'd add a comment about that fact in it) and the explanation of the quoted function on the page starts at "If you shift the the constant by 7 bits to the right." The page is a result of an edit where the older function (that ends with + c / 128;) was explained first.
Still, nice.
- deleted 11y ago[deleted]
- seiji 11y agoTowards the bottom of the article it says: the last thing I did was changing the constants to decimal - they look way more scary this way ;> because, you know, programming should be innately difficult and confusing so we can laugh at others who understand less than we do. Never underestimate how many times code (any code) posted online will be copy/pasted throughout the world everywhere from school projects to production systems.
- Confusion 11y agoThis code was already difficult and confusing and the author obviously pokes fun at his own code by decimalizing the constants. No need to accuse him of elitist or immoral behaviour.
- acqq 11y agoIt's a good and common practice to keep the constants in the sanest form and not obfuscate them, see the collections of such functions, like the already quoted http://aggregate.org/MAGIC/ http://aggregate.org/MAGIC/ or like https://graphics.stanford.edu/~seander/bithacks.html https://graphics.stanford.edu/~seander/bithacks.html By keeping the decimal constants he diminishes the chance to be attributed.
- stavrianos 11y agoSure, sure, in most contexts code as communication medium is laudable. Here, the game is to make people accustomed to understanding go "the hell just happened?" when it works. I like it.
- _yy 11y agoYeah but that does not apply to jokes.
- acqq 11y agoYou mean the whole article is a joke just because the article ends with "the last thing I did was changing the constants to decimal - they look way more scary this way"? I see it as a presentation of an efficient specialized function, just like the other bit hacks are (some already linked, one more, a whole book, here: http://www.hackersdelight.org/ http://www.hackersdelight.org/ ). If it's is to impress the uninitiated, such are more probable to not know the hex codes too. For them is the decimal number the one less scary than the hex one. For others, converting back to hex is just a tiny annoyance, nothing more.
- pavel_lishin 11y agoBut it's not efficient. The author even explicitly says so at the beginning: > (Disclaimer: It's not faster. It's not better. It's not production-quality code. It's just is and it was fun to make ;>) It's a joke, a joke with some context, that you tell other people who know the context. Would you accuse people of intentionally obfuscating things when they riff on "the door is ajar" joke?
- JoeAltmaier 11y agoCool hacks seem to be lightening rods for the humorless. There are entire contests dedicated to obfuscating code! This was a neat, unintuitive solution that he revealed in steps with a surprise ending. As an essay it worked perfectly.
- mikeash 11y agoThe whole thing is whimsy. You'd never want to use this approach at all in a production system. Using decimal constants instead of hex is the least of the sins here. There should be room in programming for whimsy. This stuff is fun, and play is healthy. If somebody takes this thing seriously and copies the code into a production system, that's their own fault, not the author's.
- acqq 11y ago> You'd never want to use this approach at all in a production system Actually it's not less useful than the code the compilers you depend on use all the time. They really depend on such "bit twiddles." Guy Steele agrees too, read his foreword to the "Hacker's Delight," a whole book about such functions: http://www.hackersdelight.org/foreword.pdf http://www.hackersdelight.org/foreword.pdf
- Splines 11y agoThe difference being that the compiler knows what sort of bit-twiddling it should be doing and what sort of bit-twiddling it should not be doing. I would not use this code without being very thoughtful and intentional about where it was being used.
- acqq 11y ago> I would not use this code without being very thoughtful and intentional about where it was being used. Sure, definitely not for everybody and every occasion. It's a platform dependent code. See my comment to cnvogel and look at his implementation for an example.
- seiji 11y agoMy job is basically fixing the code and processes of corporate programmers who copy/paste examples from websites into production systems. There's little understanding of how the code works and almost no reading of related documentation. If I compiles, I ships. It's like if bridge designers didn't really know mechanics and just copy/pasted minecraft designs into the real world. Sure, it's not the fault of minecraft designers, but we can help kill fewer people by making better examples.
- deleted 11y ago[deleted]
- EdSharkey 11y agoI wish I could do this in JavaScript.
- irascible 11y agoYou can but you have to use jquery.