KrisLibrary  1.0.0
bits.h
1 #ifndef UTILS_BITS_H
2 #define UTILS_BITS_H
3 
4 #include <KrisLibrary/errors.h>
5 
7 inline bool GetBit(unsigned int x,int index)
8 {
9  return ((x & (1 << index)) != 0);
10 }
11 
13 inline void SetBit(unsigned int& x,int index,bool value)
14 {
15  if(value) x |= (1<<index);
16  else x &= (~(1<<index));
17 }
18 
20 inline int NumBits(unsigned int x)
21 {
22  int n=0;
23  while(x)
24  {
25  ++n;
26  x &= x - 1;
27  }
28  return n;
29 }
30 
32 inline int HammingDistance(unsigned int x,unsigned int y)
33 {
34  return NumBits(x ^ y);
35 }
36 
37 inline int GetLeastBit4(unsigned int x)
38 {
39  if(x & 0x1) return 0;
40  else if(x & 0x2) return 1;
41  else if(x & 0x4) return 2;
42  else if(x & 0x8) return 3;
43  else return -1;
44 }
45 
46 inline int GetLeastBit8(unsigned int x)
47 {
48  if(x & 0xf)
49  return GetLeastBit4(x);
50  else
51  return GetLeastBit4(x>>4)+4;
52 }
53 
54 inline int GetLeastBit16(unsigned int x)
55 {
56  if(x & 0xff)
57  return GetLeastBit8(x);
58  else
59  return GetLeastBit8(x>>8)+8;
60 }
61 
64 inline int GetLeastBit(unsigned int x)
65 {
66  if(x & 0xffff)
67  return GetLeastBit16(x);
68  else
69  return GetLeastBit16(x>>16)+16;
70 }
71 
72 inline int GetGreatestBit4(unsigned int x)
73 {
74  if(x & 0x8) return 3;
75  else if(x & 0x4) return 2;
76  else if(x & 0x2) return 1;
77  else if(x & 0x1) return 0;
78  else return -1;
79 }
80 
81 inline int GetGreatestBit8(unsigned int x)
82 {
83  if(x & 0xf0)
84  return GetGreatestBit4(x>>4)+4;
85  else
86  return GetGreatestBit4(x);
87 }
88 
89 inline int GetGreatestBit16(unsigned int x)
90 {
91  if(x & 0xff00)
92  return GetGreatestBit8(x>>8)+8;
93  else
94  return GetGreatestBit8(x);
95 }
96 
99 inline int GetGreatestBit(unsigned int x)
100 {
101  if(x & 0xffff0000)
102  return GetGreatestBit16(x>>16)+16;
103  else
104  return GetGreatestBit16(x);
105 }
106 
107 
111 template <class IterT>
112 unsigned int IndicesToBits(IterT begin,IterT end)
113 {
114  unsigned int c=0;
115  while(begin != end) {
116  Assert(*begin >= 0 && *begin < 32);
117  c |= (1 << *begin);
118  ++begin;
119  }
120  return c;
121 }
122 
127 template <class IterT>
128 IterT BitsToIndices(unsigned int x,IterT begin)
129 {
130  unsigned int c=1;
131  for(int i=0;i<32;i++) {
132  if(x & c) {
133  *(begin) = i;
134  ++begin;
135  }
136  c <<= 1;
137  }
138  return begin;
139 }
140 
144 template <class IterT>
145 unsigned int IteratorToBits(IterT begin,IterT end)
146 {
147  unsigned int c=0;
148  int i=0;
149  while(begin != end) {
150  Assert(*begin >= 0 && *begin < 32);
151  if(*begin) c |= (1 << i);
152  ++begin;
153  ++i;
154  }
155  return c;
156 }
157 
163 template <class IterT>
164 void BitsToIterator(unsigned int x,IterT begin,IterT end)
165 {
166  unsigned int c=1;
167  for(int i=0;i<32;i++) {
168  if(x & c) *(begin) = 1;
169  else (*begin) = 0;
170  c <<= 1;
171  ++begin;
172  if(begin == end) break;
173  }
174  while(begin != end) {
175  *begin=0;
176  ++begin;
177  }
178 }
179 
180 #endif