generated from gemstone/gemtem
/
BitExtensions.cs
255 lines (224 loc) · 11.5 KB
/
BitExtensions.cs
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//******************************************************************************************************
// BitExtensions.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 01/25/2008 - J. Ritchie Carroll
// Initial version of source generated.
// 09/14/2009 - Stephen C. Wills
// Added new header and license agreement.
// 12/14/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
#region [ Contributor License Agreements ]
/**************************************************************************\
Copyright © 2009 - J. Ritchie Carroll, Pinal C. Patel
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\**************************************************************************/
#endregion
namespace Gemstone.Numeric.UInt24Extensions;
/// <summary>
/// Defines extension methods related to <see cref="UInt24"/> bit operations.
/// </summary>
public static class BitExtensions
{
/// <summary>
/// Returns value with specified <paramref name="bits"/> set.
/// </summary>
/// <param name="source">Value source.</param>
/// <param name="bits"><see cref="Bits"/> to set.</param>
/// <returns><see cref="UInt24"/> value with specified <paramref name="bits"/> set.</returns>
public static UInt24 SetBits(this UInt24 source, Bits bits) =>
SetBits(source, (UInt24)bits);
/// <summary>
/// Returns value with specified <paramref name="bits"/> set.
/// </summary>
/// <param name="source">Value source.</param>
/// <param name="bits">Bit-mask of the bits to set.</param>
/// <returns><see cref="UInt24"/> value with specified <paramref name="bits"/> set.</returns>
public static UInt24 SetBits(this UInt24 source, UInt24 bits) =>
source | bits;
/// <summary>
/// Returns value with specified <paramref name="bits"/> cleared.
/// </summary>
/// <param name="source">Value source.</param>
/// <param name="bits"><see cref="Bits"/> to clear.</param>
/// <returns><see cref="UInt24"/> value with specified <paramref name="bits"/> cleared.</returns>
public static UInt24 ClearBits(this UInt24 source, Bits bits) =>
ClearBits(source, (UInt24)bits);
/// <summary>
/// Returns value with specified <paramref name="bits"/> cleared.
/// </summary>
/// <param name="source">Value source.</param>
/// <param name="bits">Bit-mask of the bits to clear.</param>
/// <returns><see cref="UInt24"/> value with specified <paramref name="bits"/> cleared.</returns>
public static UInt24 ClearBits(this UInt24 source, UInt24 bits) =>
source & ~bits;
/// <summary>
/// Determines if specified <paramref name="bits"/> are set.
/// </summary>
/// <param name="source">Value source.</param>
/// <param name="bits"><see cref="Bits"/> to check.</param>
/// <returns>true if specified <paramref name="bits"/> are set in <paramref name="source"/> value; otherwise false.</returns>
public static bool CheckBits(this UInt24 source, Bits bits) =>
CheckBits(source, (UInt24)bits);
/// <summary>
/// Determines if specified <paramref name="bits"/> are set.
/// </summary>
/// <param name="source">Value source.</param>
/// <param name="bits">Bit-mask of the bits to check.</param>
/// <returns>true if specified <paramref name="bits"/> are set in <paramref name="source"/> value; otherwise false.</returns>
public static bool CheckBits(this UInt24 source, UInt24 bits) =>
CheckBits(source, bits, true);
/// <summary>
/// Determines if specified <paramref name="bits"/> are set.
/// </summary>
/// <param name="source">Value source.</param>
/// <param name="bits"><see cref="Bits"/> to check.</param>
/// <param name="allBits">true to check if all <paramref name="bits"/> are set; otherwise false.</param>
/// <returns>true if specified <paramref name="bits"/> are set in <paramref name="source"/> value; otherwise false.</returns>
public static bool CheckBits(this UInt24 source, Bits bits, bool allBits) =>
CheckBits(source, (UInt24)bits, allBits);
/// <summary>
/// Determines if specified <paramref name="bits"/> are set.
/// </summary>
/// <param name="source">Value source.</param>
/// <param name="bits">Bit-mask of the bits to check.</param>
/// <param name="allBits">true to check if all <paramref name="bits"/> are set; otherwise false.</param>
/// <returns>true if specified <paramref name="bits"/> are set in <paramref name="source"/> value; otherwise false.</returns>
public static bool CheckBits(this UInt24 source, UInt24 bits, bool allBits) =>
allBits ? (source & bits) == bits : (source & bits) != 0;
/// <summary>
/// Returns value with specified <paramref name="bits"/> toggled.
/// </summary>
/// <param name="source">Value source.</param>
/// <param name="bits"><see cref="Bits"/> to toggle.</param>
/// <returns><see cref="UInt24"/> value with specified <paramref name="bits"/> toggled.</returns>
public static UInt24 ToggleBits(this UInt24 source, Bits bits) =>
ToggleBits(source, (UInt24)bits);
/// <summary>
/// Returns value with specified <paramref name="bits"/> toggled.
/// </summary>
/// <param name="source">Value source.</param>
/// <param name="bits">Bit-mask of the bits to toggle.</param>
/// <returns><see cref="UInt24"/> value with specified <paramref name="bits"/> toggled.</returns>
public static UInt24 ToggleBits(this UInt24 source, UInt24 bits) =>
source ^ bits;
/// <summary>
/// Returns value stored in the bits represented by the specified <paramref name="bitmask"/>.
/// </summary>
/// <param name="source">Value source.</param>
/// <param name="bitmask"><see cref="Bits"/> that make-up the bit-mask.</param>
/// <returns><see cref="UInt24"/> value.</returns>
public static UInt24 GetMaskedValue(this UInt24 source, Bits bitmask) =>
GetMaskedValue(source, (UInt24)bitmask);
/// <summary>
/// Returns value stored in the bits represented by the specified <paramref name="bitmask"/>.
/// </summary>
/// <param name="source">Value source.</param>
/// <param name="bitmask">Bit-mask of the bits involved.</param>
/// <returns><see cref="UInt24"/> value.</returns>
public static UInt24 GetMaskedValue(this UInt24 source, UInt24 bitmask)
{
return source & bitmask;
}
/// <summary>
/// Returns value after setting a new <paramref name="value"/> for the bits specified by the <paramref name="bitmask"/>.
/// </summary>
/// <param name="source">Value source.</param>
/// <param name="bitmask"><see cref="Bits"/> that make-up the bit-mask.</param>
/// <param name="value">New value.</param>
/// <returns><see cref="UInt24"/> value.</returns>
public static UInt24 SetMaskedValue(this UInt24 source, Bits bitmask, UInt24 value)
{
return SetMaskedValue(source, (UInt24)bitmask, value);
}
/// <summary>
/// Returns value after setting a new <paramref name="value"/> for the bits specified by the <paramref name="bitmask"/>.
/// </summary>
/// <param name="source">Value source.</param>
/// <param name="bitmask">Bit-mask of the bits involved.</param>
/// <param name="value">New value.</param>
/// <returns><see cref="UInt24"/> value.</returns>
public static UInt24 SetMaskedValue(this UInt24 source, UInt24 bitmask, UInt24 value)
{
return (source & ~bitmask) | value;
}
/// <summary>
/// Performs rightwise bit-rotation for the specified number of rotations.
/// </summary>
/// <param name="value">Value used for bit-rotation.</param>
/// <param name="rotations">Number of rotations to perform.</param>
/// <returns>Value that has its bits rotated to the right the specified number of times.</returns>
/// <remarks>
/// Actual rotation direction is from a big-endian perspective - this is an artifact of the native
/// .NET bit shift operators. As a result bits may actually appear to rotate right on little-endian
/// architectures.
/// </remarks>
public static UInt24 BitRotL(this UInt24 value, int rotations)
{
for (int x = 1; x <= rotations % 24; x++)
{
bool hiBitSet = value.CheckBits(Bits.Bit23);
value <<= 1;
if (hiBitSet)
value = value.SetBits(Bits.Bit00);
}
return value;
}
/// <summary>
/// Performs rightwise bit-rotation for the specified number of rotations.
/// </summary>
/// <param name="value">Value used for bit-rotation.</param>
/// <param name="rotations">Number of rotations to perform.</param>
/// <returns>Value that has its bits rotated to the right the specified number of times.</returns>
/// <remarks>
/// Actual rotation direction is from a big-endian perspective - this is an artifact of the native
/// .NET bit shift operators. As a result bits may actually appear to rotate left on little-endian
/// architectures.
/// </remarks>
public static UInt24 BitRotR(this UInt24 value, int rotations)
{
for (int x = 1; x <= rotations % 24; x++)
{
bool loBitSet = value.CheckBits(Bits.Bit00);
value >>= 1;
value = loBitSet ? value.SetBits(Bits.Bit23) : value.ClearBits(Bits.Bit23);
}
return value;
}
}