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