Monday, December 12, 2011

Binary Constant Macros in C

Tom Torfs (via Jonathan Rentzsch):

I’ve been missing the lack of support for binary numeric literals in C. To get around it I wrote the following handy macros, which allows you to simply write something like:

whatever = B8(10101010);

and will translate as:

whatever = 85;

3 Comments RSS · Twitter

I believe that both gcc and clang support '0b' as a prefix for binary constants.

whatever = 0b10101010;

It's not standard C (or C++), though.

Is it a convention among the members of a hidden sect that 10101010 is 85?

For me, 01010101 is 85.

http://en.wikipedia.org/wiki/Binary_numeral_system

@bob Yes, the quoted example is wrong. B8(10101010) = 170 and B8(01010101) = 85.

Leave a Comment