-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBitArray.cpp
55 lines (43 loc) · 1.12 KB
/
BitArray.cpp
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
#include "BitArray.h"
#include <string.h>
BitArray::BitArray() :
bufferInternAllocation(true), bufferLen(1)
{
buffer = new unsigned char;
*buffer = 0;
}
BitArray::BitArray(unsigned char *buffer, size_t bufferLen) :
bufferInternAllocation(false), buffer(buffer), bufferLen(bufferLen)
{
}
BitArray::~BitArray()
{
if (bufferInternAllocation)
delete buffer;
}
bool BitArray::BitAt(int bitIndex)
{
if (bitIndex < 0 || bitIndex > (bufferLen * 8))
throw string("BitArray: Index outside the bounds of the array.\n");
return !!(buffer[bitIndex >> 3] & (0x80 >> (bitIndex & 7)));
}
void BitArray::SetBit(int bitIndex)
{
if (bitIndex < 0 || bitIndex > (bufferLen * 8))
throw string("BitArray: Index outside the bounds of the array.\n");
buffer[bitIndex >> 3] |= (0x80 >> (bitIndex & 7));
}
void BitArray::ClearBit(int bitIndex)
{
if (bitIndex < 0 || bitIndex > (bufferLen * 8))
throw string("BitArray: Index outside the bounds of the array.\n");
buffer[bitIndex >> 3] &= ~(0x80 >> (bitIndex & 7));
}
void BitArray::SetAll()
{
memset(buffer, 0xFF, bufferLen);
}
void BitArray::ClearAll()
{
memset(buffer, 0, bufferLen);
}