-
Notifications
You must be signed in to change notification settings - Fork 71
/
Bits.cs
192 lines (168 loc) · 7.08 KB
/
Bits.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
using System;
using System.ComponentModel;
using System.Linq;
namespace NetTools
{
public static class Bits
{
public static byte[] Not(byte[] bytes)
{
var result = (byte[])bytes.Clone();
for (var i = 0; i < result.Length; i++)
{
result[i] = (byte)~result[i];
}
return result;
//return bytes.Select(b => (byte)~b).ToArray();
}
public static byte[] And(byte[] A, byte[] B)
{
var result = (byte[])A.Clone();
for (var i = 0; i < A.Length; i++)
{
result[i] &= B[i];
}
return result;
//return A.Zip(B, (a, b) => (byte)(a & b)).ToArray();
}
public static byte[] Or(byte[] A, byte[] B)
{
var result = (byte[])A.Clone();
for (var i = 0; i < A.Length; i++)
{
result[i] |= B[i];
}
return result;
//return A.Zip(B, (a, b) => (byte)(a | b)).ToArray();
}
// DON'T FIX this non-intuitive behavior that returns true when A <= B,
// even if the method name means "A is Greater than or Equals B", for keeping backward compatibility.
// Fixed verison is in "NetTools.Internal" namespace "Bits" class.
[EditorBrowsable(EditorBrowsableState.Never), Obsolete("This method returns true when A<=B, not A is greater than or equal (>=) B. use LtE method to check A<=B or not.")]
public static bool GE(byte[] A, byte[] B) => LtE(A, B);
// DON'T FIX this non-intuitive behavior that returns true when A >= B,
// even if the method name means "A is Less than or Equals B", for keeping backward compatibility.
// Fixed verison is in "NetTools.Internal" namespace "Bits" class.
[EditorBrowsable(EditorBrowsableState.Never), Obsolete("This method returns true when A>=B, not A is less than or equal (<=) B. use GtE method to check A>=B or not.")]
public static bool LE(byte[] A, byte[] B) => GtE(A, B);
public static bool LtE(byte[] A, byte[] B, int offset = 0)
{
if (A == null) throw new ArgumentNullException(nameof(A));
if (B == null) throw new ArgumentNullException(nameof(B));
if (offset < 0) throw new ArgumentException("offset must be greater than or equal 0.", nameof(offset));
if (A.Length <= offset || B.Length <= offset) throw new ArgumentException("offset must be less than length of A and B.", nameof(offset));
return LtECore(A, B, offset);
}
internal static bool LtECore(byte[] A, byte[] B, int offset = 0)
{
var length = A.Length;
if (length > B.Length) length = B.Length;
for (var i = offset; i < length; i++)
{
if (A[i] != B[i]) return A[i] <= B[i];
}
return true;
}
public static bool GtE(byte[] A, byte[] B, int offset = 0)
{
if (A == null) throw new ArgumentNullException(nameof(A));
if (B == null) throw new ArgumentNullException(nameof(B));
if (offset < 0) throw new ArgumentException("offset must be greater than or equal 0.", nameof(offset));
if (A.Length <= offset || B.Length <= offset) throw new ArgumentException("offset must be less than length of A and B.", nameof(offset));
return GtECore(A, B, offset);
}
internal static bool GtECore(byte[] A, byte[] B, int offset = 0)
{
var length = A.Length;
if (length > B.Length) length = B.Length;
for (var i = offset; i < length; i++)
{
if (A[i] != B[i]) return A[i] >= B[i];
}
return true;
}
public static bool IsEqual(byte[] A, byte[] B)
{
if (A == null || B == null) { return false; }
if (A.Length != B.Length) { return false; }
return A.Zip(B, (a, b) => a == b).All(x => x == true);
}
public static byte[] GetBitMask(int sizeOfBuff, int bitLen)
{
var maskBytes = new byte[sizeOfBuff];
var bytesLen = bitLen / 8;
var bitsLen = bitLen % 8;
for (var i = 0; i < bytesLen; i++)
{
maskBytes[i] = 0xff;
}
if (bitsLen > 0) maskBytes[bytesLen] = (byte)~Enumerable.Range(1, 8 - bitsLen).Select(n => 1 << n - 1).Aggregate((a, b) => a | b);
return maskBytes;
}
/// <summary>
/// Counts the number of leading 1's in a bitmask.
/// Returns null if value is invalid as a bitmask.
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static int? GetBitMaskLength(byte[] bytes)
{
if (bytes == null) throw new ArgumentNullException(nameof(bytes));
var bitLength = 0;
var idx = 0;
// find beginning 0xFF
for (; idx < bytes.Length && bytes[idx] == 0xff; idx++) ;
bitLength = 8 * idx;
if (idx < bytes.Length)
{
switch (bytes[idx])
{
case 0xFE: bitLength += 7; break;
case 0xFC: bitLength += 6; break;
case 0xF8: bitLength += 5; break;
case 0xF0: bitLength += 4; break;
case 0xE0: bitLength += 3; break;
case 0xC0: bitLength += 2; break;
case 0x80: bitLength += 1; break;
case 0x00: break;
default: // invalid bitmask
return null;
}
// remainder must be 0x00
if (bytes.Skip(idx + 1).Any(x => x != 0x00)) return null;
}
return bitLength;
}
public static byte[] Increment(byte[] bytes)
{
if (bytes == null) throw new ArgumentNullException(nameof(bytes));
var incrementIndex = Array.FindLastIndex(bytes, x => x < byte.MaxValue);
if (incrementIndex < 0) throw new OverflowException();
return bytes
.Take(incrementIndex)
.Concat(new byte[] { (byte)(bytes[incrementIndex] + 1) })
.Concat(new byte[bytes.Length - incrementIndex - 1])
.ToArray();
}
public static byte[] Decrement(byte[] bytes)
{
if (bytes == null) throw new ArgumentNullException(nameof(bytes));
if (bytes.All(x => x == byte.MinValue)) throw new OverflowException();
var result = new byte[bytes.Length];
Array.Copy(bytes, result, bytes.Length);
for (var i = result.Length - 1; i >= 0; i--)
{
if (result[i] > byte.MinValue)
{
result[i]--;
break;
}
else
{
result[i] = byte.MaxValue;
}
}
return result;
}
}
}