-
Notifications
You must be signed in to change notification settings - Fork 33
/
Bitset.cs
115 lines (91 loc) · 2.44 KB
/
Bitset.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
using System;
using System.Collections.Generic;
namespace KSPAdvancedFlyByWire
{
public class Bitset
{
public int m_SingleData = 0;
public int[] m_Data = null;
public int m_NumBits = 0;
public Bitset() {}
public Bitset(int numBits)
{
m_NumBits = numBits;
if (numBits <= 32)
{
m_SingleData = 0;
return;
}
int numInts = numBits / 32 + ((numBits % 32 == 0) ? 0 : 1);
m_Data = new int[numInts];
for (var i = 0; i < numInts; i++)
{
m_Data[i] = 0;
}
}
public Bitset Copy()
{
Bitset result = new Bitset(m_NumBits);
if (m_NumBits <= 32)
{
result.m_SingleData = m_SingleData;
return result;
}
for(int i = 0; i < m_Data.Length; i++)
{
result.m_Data[i] = m_Data[i];
}
return result;
}
public override string ToString()
{
string result = "";
for (int i = 0; i < m_NumBits; i++)
{
if(Get(i))
{
result = "1" + result;
}
else
{
result = "0" + result;
}
}
return result;
}
public Bitset(int numBits, int initialValue)
{
m_NumBits = numBits;
if (numBits <= 32)
{
numBits = 32;
m_SingleData = initialValue;
return;
}
int numInts = numBits / 32 + ((numBits % 32 == 0) ? 0 : 1);
m_Data = new int[numInts];
for (var i = 0; i < numInts; i++)
{
m_Data[i] = 0;
}
m_Data[0] = initialValue;
}
public void Set(int bit)
{
if (m_NumBits <= 32)
{
m_SingleData |= 1 << (bit % 32);
return;
}
m_Data[bit / 32] |= 1 << (bit % 32);
}
public bool Get(int bit)
{
if (m_NumBits <= 32)
{
return (m_SingleData & (1 << (bit % 32))) != 0;
}
return (m_Data[bit / 32] & (1 << (bit % 32))) != 0;
}
}
}