-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDAABitArray.cs
190 lines (164 loc) · 5.38 KB
/
DAABitArray.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Asgn
{
/// <summary>
/// Class to handle an array of bits
///
/// </summary>
public class DAABitArray
{
private List<bool> m_bits;
/// <summary>
/// Initialises an empty bit array
/// </summary>
public DAABitArray()
{
m_bits = new List<bool>();
}
/// <summary>
/// Copy constructor
/// </summary>
public DAABitArray(DAABitArray other)
{
m_bits = new List<bool>(other.m_bits);
}
/// <summary>
/// Append a single bit to the end of the bit array.
/// </summary>
/// <param name="bitVal">The bit to append.</param>
public void Append(bool bitVal)
{
// Append the new bit
m_bits.Add(bitVal);
}
/// <summary>
/// Append a single bit to the end of the bit array.
/// </summary>
/// <param name="bitVal">The bit to append.</param>
public void Append(int bitVal)
{
// Append the new bit
if (bitVal == 0)
m_bits.Add(false);
else
m_bits.Add(true);
}
/// <summary>
/// Append another bit array to the end of this bit array.
/// </summary>
/// <param name="bitSet">The bit array to append.</param>
public void Append(DAABitArray bitSet)
{
for (int ii = 0; ii < bitSet.NumBits; ii++) {
m_bits.Add(bitSet.GetBitAsBool(ii));
}
}
/// <summary>
/// Append a set of bits in a long to the end of the bit array.
/// </summary>
/// <param name="bitSet">The set of bits to append.</param>
/// <param name="numBits">The number of bits from bitSet to append.</param>
public void Append(long bitSet, int numBits)
{
long mask = 0x01;
// Start appending the most-significant bit first
mask = mask << (numBits-1);
// Append the new bits
for (int ii = 0; ii < numBits; ii++) {
if ((mask & bitSet) != 0)
m_bits.Add(true);
else
m_bits.Add(false);
// Shift to next bit in the long
mask = mask >> 1;
}
}
/// <summary>
/// Clone (deep copy) this bit array.
/// </summary>
public DAABitArray Clone()
{
return new DAABitArray(this);
}
/// <summary>
/// Delete the last bit from the bit array.
/// </summary>
public void RemoveLastBit()
{
// Remove the last bit added
m_bits.RemoveAt(m_bits.Count - 1);
}
/// <summary>
/// Reverse the bit array.
/// </summary>
public void Reverse()
{
m_bits.Reverse();
}
/// <summary>
/// Number of bits in the bit array.
/// </summary>
public int NumBits
{
get { return m_bits.Count; }
}
/// <summary>
/// Get a bit as a Boolean value.
/// </summary>
/// <param name="bitIndex">Index of the bit to get (zero-based).</param>
/// <returns>The bit value at bitIndex.</returns>
public bool GetBitAsBool(int bitIndex)
{
return m_bits[bitIndex];
}
/// <summary>
/// Get a bit as a Boolean value, using indexing [] notation.
/// </summary>
/// <value>Index of the bit to get (zero-based).</value>
/// <returns>The bit value at bitIndex.</returns>
public bool this[int bitIndex]
{
get { return m_bits[bitIndex]; }
}
/// <summary>
/// Get a bit as a long int value.
/// </summary>
/// <param name="bitIndex">Index of the bit to get (zero-based).</param>
/// <returns>The bit value at bitIndex.</returns>
public long GetBitAsLong(int bitIndex)
{
bool bThisBit = m_bits[bitIndex];
if (bThisBit)
return 0x01;
else
return 0x00;
}
/// <summary>
/// Extract a range of bit as a long int value.
/// </summary>
/// <param name="startBitIndex">Index of the first bit in the range (zero-based).</param>
/// <param name="endBitIndex">Index of the last bit in the range (zero-based).</param>
/// <returns>The long containing the bits requested.</returns>
public long GetBitRange(int startBitIdx, int endBitIdx)
{
int iNumBits;
long iBitSetVal;
iNumBits = endBitIdx - startBitIdx + 1;
if (iNumBits > (sizeof(long) * 8))
throw new ArgumentOutOfRangeException("Max number of bits is " + sizeof(long) * 8);
iBitSetVal = 0;
for (int ii = startBitIdx; ii <= endBitIdx; ii++) {
// Make room for the next bit
iBitSetVal = iBitSetVal << 1;
if (m_bits[ii] == true) {
iBitSetVal = iBitSetVal | 0x01;
}
// else the bit shift << will have 'added' a 0 on the end anyways
}
return iBitSetVal;
}
}
}