-
Notifications
You must be signed in to change notification settings - Fork 13
/
bithacks.h
73 lines (54 loc) · 1.81 KB
/
bithacks.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
** bithacks.h - bit hacks macros. v1.0
**
** Peteris Krumins (peter@catonmat.net)
** http://www.catonmat.net -- good coders code, great reuse
**
** This file is explained in the following article:
** http://www.catonmat.net/blog/bit-hacks-header-file
**
** Released under the MIT license.
*/
#ifndef BITHACKS_H
#define BITHACKS_H
#define HEXIFY(X) 0x##X##LU
#define B8IFY(Y) (((Y&0x0000000FLU)?1:0) + \
((Y&0x000000F0LU)?2:0) + \
((Y&0x00000F00LU)?4:0) + \
((Y&0x0000F000LU)?8:0) + \
((Y&0x000F0000LU)?16:0) + \
((Y&0x00F00000LU)?32:0) + \
((Y&0x0F000000LU)?64:0) + \
((Y&0xF0000000LU)?128:0))
#define B8(Z) ((unsigned char)B8IFY(HEXIFY(Z)))
/*
** Bit hack routines. See the following article for explanation:
** http://www.catonmat.net/blog/low-level-bit-hacks-you-absolutely-must-know
**
*/
/* test if x is even */
#define B_EVEN(x) (((x)&1)==0)
/* test if x is odd */
#define B_ODD(x) (!B_EVEN((x)))
/* test if n-th bit in x is set */
#define B_IS_SET(x, n) (((x) & (1<<(n)))?1:0)
/* set n-th bit in x */
#define B_SET(x, n) ((x) |= (1<<(n)))
/* unset n-th bit in x */
#define B_UNSET(x, n) ((x) &= ~(1<<(n)))
/* toggle n-th bit in x */
#define B_TOGGLE(x, n) ((x) ^= (1<<(n)))
/* turn off right-most 1-bit in x */
#define B_TURNOFF_1(x) ((x) &= ((x)-1))
/* isolate right-most 1-bit in x */
#define B_ISOLATE_1(x) ((x) &= (-(x)))
/* right-propagate right-most 1-bit in x */
#define B_PROPAGATE_1(x) ((x) |= ((x)-1))
/* isolate right-most 0-bit in x */
#define B_ISOLATE_0(x) ((x) = ~(x) & ((x)+1))
/* turn on right-most 0-bit in x */
#define B_TURNON_0(x) ((x) |= ((x)+1))
/*
** more bit hacks coming as soon as I post an article on advanced bit hacks
*/
#endif