-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBits.h
More file actions
58 lines (49 loc) · 1.32 KB
/
Bits.h
File metadata and controls
58 lines (49 loc) · 1.32 KB
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
#pragma once
#ifndef BITS_H
#define BITS_H
// Contains helping methods to with with bits in dword (<see cref="UInt32" />).
class Bits
{
public:
/// <summary>
/// Returns number of leading zero bits in int.
/// </summary>
/// <param name="x">Int value.</param>
/// <returns>Number of leading zero bits.</returns>
static int Nlz(const unsigned int value)
{
unsigned int x = value;
if (x == 0) return 32;
int n = 1;
if ((x >> 16) == 0) { n += 16; x <<= 16; }
if ((x >> 24) == 0) { n += 8; x <<= 8; }
if ((x >> 28) == 0) { n += 4; x <<= 4; }
if ((x >> 30) == 0) { n += 2; x <<= 2; }
return n - (int)(x >> 31);
} // end function Nlz
/// <summary>
/// Counts position of the most significant bit in int.
/// Can also be used as Floor(Log2(<paramref name="x" />)).
/// </summary>
/// <param name="x">Int value.</param>
/// <returns>Position of the most significant one bit (-1 if all zeroes).</returns>
static int Msb(const unsigned int x)
{
return 31 - Nlz(x);
} // end function Msb
/// <summary>
/// Ceil(Log2(<paramref name="x" />)).
/// </summary>
/// <param name="x">Int value.</param>
/// <returns>Ceil of the Log2.</returns>
static int CeilLog2(const unsigned int x)
{
int msb = Msb(x);
if (x != 1U << msb)
{
++msb;
}
return msb;
} // end function CeilLog2
}; // end class Bits
#endif