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.