]> git.piffa.net Git - sketchbook_andrea/blob - libraries/AVR-Programming-Library/binaryMacro.h
9dff3c1e62a146659cc1df04517aeacd268b5955
[sketchbook_andrea] / libraries / AVR-Programming-Library / binaryMacro.h
1 /* Binary constant generator macro
2 By Tom Torfs - donated to the public domain
3 */
4
5 /* All macro's evaluate to compile-time constants */
6
7 /* *** helper macros *** /
8
9 /* turn a numeric literal into a hex constant
10 (avoids problems with leading zeroes)
11 8-bit constants max value 0x11111111, always fits in unsigned long
12 */
13 #define HEX__(n) 0x##n##LU
14
15 /* 8-bit conversion function */
16 #define B8__(x) ((x&0x0000000FLU)?1:0)  \
17 +((x&0x000000F0LU)?2:0) \
18 +((x&0x00000F00LU)?4:0) \
19 +((x&0x0000F000LU)?8:0) \
20 +((x&0x000F0000LU)?16:0)        \
21 +((x&0x00F00000LU)?32:0)        \
22 +((x&0x0F000000LU)?64:0)        \
23 +((x&0xF0000000LU)?128:0)
24
25 /* *** user macros *** /
26
27 /* for upto 8-bit binary constants */
28 #define B8(d) ((unsigned char)B8__(HEX__(d)))
29
30 /* for upto 16-bit binary constants, MSB first */
31 #define B16(dmsb,dlsb) (((unsigned short)B8(dmsb)<<8)   \
32 + B8(dlsb))
33
34 /* for upto 32-bit binary constants, MSB first */
35 #define B32(dmsb,db2,db3,dlsb) (((unsigned long)B8(dmsb)<<24)    \
36 + ((unsigned long)B8(db2)<<16) \
37 + ((unsigned long)B8(db3)<<8)    \
38 + B8(dlsb))
39
40 /* Sample usage:
41 B8(01010101) = 85
42 B16(10101010,01010101) = 43605
43 B32(10000000,11111111,10101010,01010101) = 2164238933
44 */
45