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
|
public static class Utils
{
/// <summary>
/// Rotates the given int value right by the specified number of bits.
/// </summary>
/// <param name="number">The integer to rotate</param>
/// <param name="distance">The number of bits to rotate</param>
/// <returns>Returns the given int rotated right side by the given distance</returns>
public static int RotateRight(int i, int distance)
{
uint num = (uint)i;
int length = (sizeof(int) * 8);
distance = distance % length;
uint add = num << (length - distance);
num = num >> distance;
num = num | add;
return (int)num;
}
/// <summary>
/// Rotates the given int value left by the specified number of bits.
/// </summary>
/// <param name="number">The integer to rotate</param>
/// <param name="distance">The number of bits to rotate</param>
/// <returns>Returns the given int rotated left side by the given distance</returns>
public static int RotateLeft(int i, int distance)
{
uint num = (uint)i;
int length = (sizeof(int) * 8);
distance = distance % length;
uint add = num >> (length - distance);
num = num << distance;
num = num | add;
return (int)num;
}
}
|