9 ms·
Here's one of my all-time favorite way to write binary literals in C by abusing define: #define HEXIFY(X) 0x##X##LU #define B8IFY(Y) (((Y&0x0000000FLU
by pkrumins 13y ago
Here's one of my all-time favorite way to write binary literals in C by abusing define:
#define HEXIFY(X) 0x##X##LU
#define B8IFY(Y) (((Y&0x0000000FLU)?1:0) + \
((Y&0x000000F0LU)?2:0) + \
((Y&0x00000F00LU)?4:0) + \
((Y&0x0000F000LU)?8:0) + \
((Y&0x000F0000LU)?16:0) + \
((Y&0x00F00000LU)?32:0) + \
((Y&0x0F000000LU)?64:0) + \
((Y&0xF0000000LU)?128:0))
#define B8(Z) ((unsigned char)B8IFY(HEXIFY(Z)))
And now you can write:
B(01011010)
Etc.
If you like this, see my blog post about bithacks.h:
http://www.catonmat.net/blog/bit-hacks-header-file/ http://www.catonmat.net/blog/bit-hacks-header-file/
- dbaupp 13y agoUsing octal (0o) gives you larger representable numbers, fwiw.