4 #include <KrisLibrary/errors.h> 7 inline bool GetBit(
unsigned int x,
int index)
9 return ((x & (1 << index)) != 0);
13 inline void SetBit(
unsigned int& x,
int index,
bool value)
15 if(value) x |= (1<<index);
16 else x &= (~(1<<index));
20 inline int NumBits(
unsigned int x)
32 inline int HammingDistance(
unsigned int x,
unsigned int y)
34 return NumBits(x ^ y);
37 inline int GetLeastBit4(
unsigned int x)
40 else if(x & 0x2)
return 1;
41 else if(x & 0x4)
return 2;
42 else if(x & 0x8)
return 3;
46 inline int GetLeastBit8(
unsigned int x)
49 return GetLeastBit4(x);
51 return GetLeastBit4(x>>4)+4;
54 inline int GetLeastBit16(
unsigned int x)
57 return GetLeastBit8(x);
59 return GetLeastBit8(x>>8)+8;
64 inline int GetLeastBit(
unsigned int x)
67 return GetLeastBit16(x);
69 return GetLeastBit16(x>>16)+16;
72 inline int GetGreatestBit4(
unsigned int x)
75 else if(x & 0x4)
return 2;
76 else if(x & 0x2)
return 1;
77 else if(x & 0x1)
return 0;
81 inline int GetGreatestBit8(
unsigned int x)
84 return GetGreatestBit4(x>>4)+4;
86 return GetGreatestBit4(x);
89 inline int GetGreatestBit16(
unsigned int x)
92 return GetGreatestBit8(x>>8)+8;
94 return GetGreatestBit8(x);
99 inline int GetGreatestBit(
unsigned int x)
102 return GetGreatestBit16(x>>16)+16;
104 return GetGreatestBit16(x);
111 template <
class IterT>
112 unsigned int IndicesToBits(IterT begin,IterT end)
115 while(begin != end) {
116 Assert(*begin >= 0 && *begin < 32);
127 template <
class IterT>
128 IterT BitsToIndices(
unsigned int x,IterT begin)
131 for(
int i=0;i<32;i++) {
144 template <
class IterT>
145 unsigned int IteratorToBits(IterT begin,IterT end)
149 while(begin != end) {
150 Assert(*begin >= 0 && *begin < 32);
151 if(*begin) c |= (1 << i);
163 template <
class IterT>
164 void BitsToIterator(
unsigned int x,IterT begin,IterT end)
167 for(
int i=0;i<32;i++) {
168 if(x & c) *(begin) = 1;
172 if(begin == end)
break;
174 while(begin != end) {