-
Notifications
You must be signed in to change notification settings - Fork 1
/
dynbitfield.cs
87 lines (68 loc) · 1.92 KB
/
dynbitfield.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
using System.Collections.Generic;
using System.Linq;
namespace ndot
{
public struct dynbitfield
{
public List<byte> bytes;
public uint Length { get => (uint)bytes.Count; /*set => bytes = new byte[value >= 0 ? value : 0];*/ }
public uint Size { get => (Length) * 8; /*set => bytes = new byte[value / 8 >= 0 ? value / 8 : 0];*/ }
public bool this[int index]
{
get
{
return get(index);
}
set
{
set(value, index);
}
}
public void set(bool value, int at)
{
var bi = 7 - at % 8;
at /= 8;
if (at >= Length)
{
bytes.Add(0);
}
byte mask = (byte)(1 << bi);
if (value)
bytes[at] |= mask;
else
bytes[at] &= (byte)~mask;
}
public bool get(int at)
{
var bi = 7 - at % 8;
at /= 8;
byte mask = (byte)(1 << bi);
return (bytes[at] & mask) == mask;
}
public byte[] ReversedArray()
{
byte[] rev = new byte[Length];
for (var i = (int)Length; i > 0; i--)
{
rev[Length - i] = bytes.ElementAt(i - 1);
}
return rev;
}
public override string ToString()
{
string s = "";
//for (int i = (int)Size - 1; i >= 0; i--)
//{
//}
for (int i = 0; i < Size; i++)
{
s += (get(i) ? "1" : "0");
if ((i + 1) % 8 == 0)
s += " ";
if ((i + 1) % 64 == 0)
s += "\r\n";
}
return s;
}
}
}