-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBooleanBitWrapper.cpp
More file actions
151 lines (124 loc) · 3.33 KB
/
BooleanBitWrapper.cpp
File metadata and controls
151 lines (124 loc) · 3.33 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
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
#include "BooleanBitWrapper.hpp"
namespace TwilightDream
{
/* BooleanBitWrapper */
BooleanBitWrapper::BooleanBitWrapper() : bits( 0 ) {}
BooleanBitWrapper::BooleanBitWrapper( uint32_t value ) : bits( value ) {}
BooleanBitWrapper::~BooleanBitWrapper()
{
bits = 0;
}
// 按位与操作
void BooleanBitWrapper::bit_and( uint32_t other )
{
bits &= other;
}
// 按位或操作
void BooleanBitWrapper::bit_or( uint32_t other )
{
bits |= other;
}
// 按位非操作
void BooleanBitWrapper::bit_not()
{
bits = ~bits;
}
// 按位异或操作
void BooleanBitWrapper::bit_xor( uint32_t other )
{
bits ^= other;
}
// 按位同或操作
void BooleanBitWrapper::bit_not_xor( uint32_t other )
{
bits = ~( bits ^ other );
}
// 按位非与操作
void BooleanBitWrapper::bit_not_and( uint32_t other )
{
bits = ~( bits & other );
}
// 按位非或操作
void BooleanBitWrapper::bit_not_or( uint32_t other )
{
bits = ~( bits | other );
}
// 按位左移操作
void BooleanBitWrapper::bit_leftshift( int shift )
{
bits <<= shift;
}
// 按位右移操作
void BooleanBitWrapper::bit_rightshift( int shift )
{
bits >>= shift;
}
// 设置所有位为给定的布尔值
void BooleanBitWrapper::bit_set( bool value )
{
bits = value ? 0xFFFFFFFF : 0;
}
// 设置指定索引的位为给定的布尔值
void BooleanBitWrapper::bit_set( bool value, int index )
{
if ( value )
{
bits |= ( 1 << index );
}
else
{
bits &= ~( 1 << index );
}
}
// 翻转指定索引的位
void BooleanBitWrapper::bit_flip( size_t index )
{
bits ^= ( 1 << index );
}
// 获取指定索引的位的布尔值
bool BooleanBitWrapper::bit_get( int index ) const
{
return ( bits >> index ) & 1;
}
// 统计比特'1'的数量
size_t BooleanBitWrapper::count_bits() const
{
uint32_t n = bits;
// 将相邻的位分组,每两位一组,然后用这两位中较低的一位表示这一组中置位的数量(0或1或2)
// 例如: 0b1101 (原始数值) 变成 0b0100
n = n - ( ( n >> 1 ) & 0x55555555 );
// 将相邻的两组位(即4位)合并为一组,然后用这一组中较低的两位表示这一组中置位的数量(0到4)
// 例如: 0b0100 (来自上一步) 变成 0b0010
n = ( n & 0x33333333 ) + ( ( n >> 2 ) & 0x33333333 );
// 将相邻的两组位(即8位)合并为一组,然后用这一组中较低的4位表示这一组中置位的数量(0到8)
// 并且我们通过和 0x0F0F0F0F 相与,消除了不需要的位
n = ( n + ( n >> 4 ) ) & 0x0F0F0F0F;
// 将32位数中的所有8位组合并,得到一个8位数,这个8位数的低8位表示原32位数中置位的数量(0到32)
n = n + ( n >> 8 );
// 同上,但这次是将两个8位数合并为一个16位数
n = n + ( n >> 16 );
// 使用与操作消除不需要的位,返回计数结果
return n & 0x3F;
}
BooleanBitWrapper::operator uint32_t() const noexcept
{
return bits;
}
BooleanBitWrapper BooleanBitWrapper::operator=( const BooleanBitWrapper& other )
{
if ( this == &other )
{
return *this;
}
bits = other.bits;
return *this;
}
bool operator==( const BooleanBitWrapper& left, const BooleanBitWrapper& right )
{
return left.bits == right.bits;
}
bool operator!=( const BooleanBitWrapper& left, const BooleanBitWrapper& right )
{
return left.bits != right.bits;
}
}