FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
IntXLib/IntXLib/Bits.cs at master · devoyster/IntXLib · GitHub
devoyster
/
IntXLib
Public
Notifications
You must be signed in to change notification settings
Fork
3
Star
43
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
IntXLib
/
IntXLib
/
Bits.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
54 lines (50 loc) · 1.33 KB
Breadcrumbs
IntXLib
/
IntXLib
/
Bits.cs
Copy path
File metadata and controls
54 lines (50 loc) · 1.33 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
using
System
;
namespace
IntXLib
{
/// <summary>
/// Contains helping methods to with with bits in dword (<see cref="UInt32" />).
/// </summary>
[
CLSCompliant
(
false
)
]
static
public
class
Bits
{
/// <summary>
/// Returns number of leading zero bits in int.
/// </summary>
/// <param name="x">Int value.</param>
/// <returns>Number of leading zero bits.</returns>
static
public
int
Nlz
(
uint
x
)
{
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
)
;
}
/// <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
public
int
Msb
(
uint
x
)
{
return
31
-
Nlz
(
x
)
;
}
/// <summary>
/// Ceil(Log2(<paramref name="x" />)).
/// </summary>
/// <param name="x">Int value.</param>
/// <returns>Ceil of the Log2.</returns>
static
public
int
CeilLog2
(
uint
x
)
{
int
msb
=
Msb
(
x
)
;
if
(
x
!=
1U
<<
msb
)
{
++
msb
;
}
return
msb
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL