Re: [OFFTOPIC] Binary declaration

Sitaram Iyer (
Mon, 7 Dec 1998 10:38:29 -0600

Thus, Mike A. Harris writes:
> >Hex: 0x<NUMBER>
> >Octal: 0<NUMBER>
> >Binary: ????

> In ANSI C, there is no binary representation available. Watcom C
> I believe has an extention to allow you to use something like:
> 0b010101110
> #define b00000000 0x00
> ...
> #define b11111111 0xFF

I once wrote a set of macros for the same, which works for 8/16/32/64 bits.
Remember to compile with -O or more, so that these get optimized away
entirely and replaced by the value (see the assembly code generated)
(without -On the generated code looks horrible). e.g. -
char x = BIN8(00101101);
short x = BIN16(0000000001001001);
int y = BIN32(00000100000010000000100000010000)
You need all the leading zeroes, so you can't say BIN8(101101) for the above.
A minor catch is that you can't use this in a declaration outside of any
function, since gcc complains that the initializer element isn't constant.

Sorry to be off-topic, but thought this might be useful.

* called by the below four macros, and they work by calling each other
* remember to compile with -On
#define _BIN1(n,i) ((#n)[i]-'0')
#define _BIN2(n,i) (_BIN1(n,i+1) + 2 * _BIN1(n,i))
#define _BIN4(n,i) (_BIN2(n,i+2) + 4 * _BIN2(n,i))
#define _BIN8(n,i) (_BIN4(n,i+4) + 16 * _BIN4(n,i))
#define _BIN16(n,i) (_BIN8(n,i+8) + 256 * _BIN8(n,i))
#define _BIN32(n,i) (_BIN16(n,i+16) + 65536UL * _BIN16(n,i))
#define _BIN64(n,i) (_BIN32(n,i+32) + 65536UL * 65536UL * _BIN32(n,i))
* Use one of the four macros below
#define BIN8(n) _BIN8(n,0)
#define BIN16(n) _BIN16(n,0)
#define BIN32(n) _BIN32(n,0)
#define BIN64(n) _BIN32(n,0)

Sitaram Iyer <>
Phone: Res: (713) 630-9260, Office: (713) 527-8750 x3291 (direct dial)
New graduate student in the Computer Science dept at Rice University

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to Please read the FAQ at