-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitwiseOperation.h
115 lines (95 loc) · 2.76 KB
/
BitwiseOperation.h
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
/*
* Copyright (C) fastman92 <[email protected]>, website: http://fastman92.com
* Licensed under the MIT License, see LICENSE at top level directory.
*
*/
/*
Author: fastman92
Description: Bitwise operations.
*/
#pragma once
namespace BitwiseOperation
{
// Changes n bits on specified position to new value.
inline unsigned int ChangeMultipleBits(unsigned int num, char position, char numberOfBits, unsigned int newValue)
{
unsigned int mask = (1 << numberOfBits)-1;
return num & ~(mask << position) | ((newValue & mask) << position);
}
// Clears multiple bits
inline unsigned int SetMultipleBits(unsigned int num, char position, char numberOfBits)
{
unsigned int mask = (1 << numberOfBits)-1;
return num | (mask << position);
}
// Clears multiple bits
inline unsigned int ClearMultipleBits(unsigned int num, char position, char numberOfBits)
{
unsigned int mask = (1 << numberOfBits)-1;
return num & ~(mask << position);
}
// Reads value from bits
inline unsigned int ReadValueFromBits(unsigned int num, char position, char numberOfBits)
{
unsigned int mask = (1 << numberOfBits)-1;
return (num >> position) & mask;
}
// Sets one bit
inline unsigned int SetOneBit(unsigned int num, char position)
{
return num | (1 << position);
}
// Clears one bit
inline unsigned int ClearOneBit(unsigned int num, char position)
{
return num & ~(1 << position);
}
// Changes one bit
inline unsigned int ChangeOneBit(unsigned int num, char position, bool newValue)
{
return num & ~(1 << position) | (newValue << position);
}
// Returns one bit
inline bool TestBit(unsigned int num, char position)
{
return (num & (1 << position)) != 0;
}
// Returns shift from value, for example
inline int GetShiftFromValue(unsigned int num)
{
const int numberOfBits = sizeof(unsigned int) * 8;
unsigned int mask = 1;
for (int i = 0; i < numberOfBits; i++)
{
if (num & mask)
{
if (num & (~mask))
return -1;
else
return i;
}
mask <<= 1;
}
return -1;
}
// Replaces bit positions
template<class tNum> inline
tNum ReplaceBitPositions(char* PositionArray, tNum num)
{
char i = sizeof(num) * 8 - 1;
tNum newNum = 0;
do
{
newNum |= (num & (1 << (i))) << (PositionArray[i] - i);
newNum |= (num & (1 << (i - 1))) << (PositionArray[i - 1] - i + 1);
newNum |= (num & (1 << (i - 2))) << (PositionArray[i - 2] - i + 2);
newNum |= (num & (1 << (i - 3))) << (PositionArray[i - 3] - i + 3);
newNum |= (num & (1 << (i - 4))) << (PositionArray[i - 4] - i + 4);
newNum |= (num & (1 << (i - 5))) << (PositionArray[i - 5] - i + 5);
newNum |= (num & (1 << (i - 6))) << (PositionArray[i - 6] - i + 6);
newNum |= (num & (1 << (i - 7))) << (PositionArray[i - 7] - i + 7);
i -= 8;
} while (i >= 0);
return newNum;
}
}