-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitUtils.cs
285 lines (235 loc) · 6.19 KB
/
BitUtils.cs
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
namespace Sylphe.Utils
{
/// <summary>
/// Methods for bit shifting, bit counting, etc., exploiting the
/// binary nature of the computer. Many algorithms here are taken
/// and/or adapted from "Hacker's Delight" by Henry S. Warren, Jr.
/// The C# Language Reference defines the bit shifting operators.
/// </summary>
public static class BitUtils
{
#region Unsigned (aka logical) right shift
// C# does not have Java's unsigned right shift (>>>) operator.
// Instead, C# does unsigned right shift on uint and ulong, but
// signed right shift (sign extension) on int and long.
// C# as well as Java use only the low order 5 (int) or 6 (long)
// bits of the shift count: x << n and x >> n shift by n&31 or n&63
// bits to the left or right. This is different from C, where shifting
// by a value beyond the word size is explicitly undefined.
public static int LogicalShiftRight(int x, int n)
{
return unchecked((int) ((uint) x >> n));
}
public static long LogicalShiftRight(long x, int n)
{
return unchecked((long) ((ulong) x >> n));
}
#endregion
#region Population count
// Population count algorithm for 32 bits is from Hacker's Delight.
// The variation for 64 bits is an adaptation of this algorithm.
// Population count is the number of one bits in the argument.
public static int PopulationCount(int value)
{
return PopulationCount(unchecked((uint) value));
}
public static int PopulationCount(uint x)
{
x = x - ((x >> 1) & 0x55555555);
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
x = (x + (x >> 4)) & 0x0F0F0F0F;
x = x + (x >> 8);
x = x + (x >> 16);
return (int) (x & 0x0000003F);
}
public static int PopulationCount(long value)
{
return PopulationCount(unchecked((ulong) value));
}
public static int PopulationCount(ulong x)
{
x = x - ((x >> 1) & 0x5555555555555555L);
x = (x & 0x3333333333333333L) + ((x >> 2) & 0x3333333333333333L);
x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0FL;
x = x + (x >> 8);
x = x + (x >> 16);
x = x + (x >> 32);
return ((int) x) & 0x7F;
}
#endregion
#region Leading and trailing zeros
// Number of leading and trailing zeros:
// Easy using PopulationCount, but other
// (slightly more efficient) procedures exist.
public static int LeadingZeroCount(int x)
{
return LeadingZeroCount(unchecked((uint) x));
}
public static int LeadingZeroCount(uint x)
{
x = x | (x >> 1);
x = x | (x >> 2);
x = x | (x >> 4);
x = x | (x >> 8);
x = x | (x >> 16);
return PopulationCount(~x);
}
public static int LeadingZeroCount(long x)
{
return LeadingZeroCount(unchecked((ulong) x));
}
public static int LeadingZeroCount(ulong x)
{
x = x | (x >> 1);
x = x | (x >> 2);
x = x | (x >> 4);
x = x | (x >> 8);
x = x | (x >> 16);
x = x | (x >> 32);
return PopulationCount(~x);
}
public static int TrailingZeroCount(int x)
{
return TrailingZeroCount(unchecked((uint) x));
}
public static int TrailingZeroCount(uint x)
{
x = x | (x << 1);
x = x | (x << 2);
x = x | (x << 4);
x = x | (x << 8);
x = x | (x << 16);
return PopulationCount(~x);
}
public static int TrailingZeroCount(long x)
{
return TrailingZeroCount(unchecked((ulong) x));
}
public static int TrailingZeroCount(ulong x)
{
x = x | (x << 1);
x = x | (x << 2);
x = x | (x << 4);
x = x | (x << 8);
x = x | (x << 16);
x = x | (x << 32);
return PopulationCount(~x);
}
#endregion
#region Power of two: test, floor, ceiling
// A power of 2 has a population count of 1.
// The methods here test if an integer is a power of two,
// and round up (ceiling) or down (floor) to a power of two.
public static bool IsPowerOfTwo(int x)
{
return (x & (x - 1)) == 0;
}
public static bool IsPowerOfTwo(uint x)
{
return (x & (x - 1)) == 0;
}
public static bool IsPowerOfTwo(long x)
{
return (x & (x - 1)) == 0;
}
public static bool IsPowerOfTwo(ulong x)
{
return (x & (x - 1)) == 0;
}
public static int PowerOfTwoCeiling(int x)
{
return unchecked((int) PowerOfTwoCeiling((uint) x));
}
public static uint PowerOfTwoCeiling(uint x)
{
x -= 1;
x |= (x >> 1);
x |= (x >> 2);
x |= (x >> 4);
x |= (x >> 8);
x |= (x >> 16);
return x + 1;
}
public static long PowerOfTwoCeiling(long x)
{
return unchecked((long) PowerOfTwoCeiling((ulong) x));
}
public static ulong PowerOfTwoCeiling(ulong x)
{
x -= 1;
x |= (x >> 1);
x |= (x >> 2);
x |= (x >> 4);
x |= (x >> 8);
x |= (x >> 16);
x |= (x >> 32);
return x + 1;
}
public static int PowerOfTwoFloor(int x)
{
return unchecked((int) PowerOfTwoFloor((uint) x));
}
public static uint PowerOfTwoFloor(uint x)
{
x |= (x >> 1);
x |= (x >> 2);
x |= (x >> 4);
x |= (x >> 8);
x |= (x >> 16);
return x - (x >> 1);
}
public static long PowerOfTwoFloor(long x)
{
return unchecked((long) PowerOfTwoFloor((ulong) x));
}
public static ulong PowerOfTwoFloor(ulong x)
{
x |= (x >> 1);
x |= (x >> 2);
x |= (x >> 4);
x |= (x >> 8);
x |= (x >> 16);
x |= (x >> 32);
return x - (x >> 1);
}
#endregion
public static string ToString(int x, bool tight = false)
{
return ToString(unchecked((uint) x), tight);
}
public static string ToString(uint x, bool tight = false)
{
int len = 32 + (tight ? 0 : 3);
var buf = new char[len];
int bufpos = 0, bitpos = 31;
while (bitpos >= 0)
{
uint mask = 1U << bitpos;
buf[bufpos++] = (x & mask) == mask ? '1' : '0';
if (!tight && (bitpos & 7) == 0 && bitpos > 0)
buf[bufpos++] = ' ';
bitpos -= 1;
}
return new string(buf, 0, buf.Length);
}
public static string ToString(long x, bool tight = false)
{
return ToString(unchecked((ulong) x), tight);
}
public static string ToString(ulong x, bool tight = false)
{
int len = 64 + (tight ? 0 : 7);
var buf = new char[len];
int bufpos = 0, bitpos = 63;
while (bitpos >= 0)
{
ulong mask = 1UL << bitpos;
buf[bufpos++] = (x & mask) == mask ? '1' : '0';
if (!tight && (bitpos & 7) == 0 && bitpos > 0)
buf[bufpos++] = ' ';
bitpos -= 1;
}
return new string(buf, 0, buf.Length);
}
}
}