1 #ifndef __BINARYCONST_H__
2 #define __BINARYCONST_H__
3 
4 /* binaryconst.h was integrated into the Dillo project in April 2004, and
5  * presumably comes from the ancestor of the code found at
6  * http://cprog.tomsweb.net/binconst.txt
7  */
8 
9 /* Macros for allowing binary constants in C
10  * By Tom Torfs - donated to the public domain */
11 
12 #define HEX__(n) 0x##n##LU
13 
14 /* 8-bit conversion function */
15 #define B8__(x) ((x&0x0000000FLU)?1:0)  \
16                +((x&0x000000F0LU)?2:0)  \
17                +((x&0x00000F00LU)?4:0)  \
18                +((x&0x0000F000LU)?8:0)  \
19                +((x&0x000F0000LU)?16:0) \
20                +((x&0x00F00000LU)?32:0) \
21                +((x&0x0F000000LU)?64:0) \
22                +((x&0xF0000000LU)?128:0)
23 
24 
25 /*
26  * *** USER MACROS ***
27  */
28 
29 /* for upto 8-bit binary constants */
30 #define B8(d) ((unsigned char)B8__(HEX__(d)))
31 
32 /* for upto 16-bit binary constants, MSB first */
33 #define B16(dmsb,dlsb) (((unsigned short)B8(dmsb)<<8) + B8(dlsb))
34 
35 /*
36  * Sample usage:
37  *    B8(01010101) = 85
38  *    B16(10101010,01010101) = 43605
39  */
40 
41 
42 #endif /* __BINARYCONST_H__ */
43 
44