Hacker News new | past | comments | ask | show | jobs | submit login

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/




Using octal (0o) gives you larger representable numbers, fwiw.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: