-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitArray.cpp
51 lines (49 loc) · 973 Bytes
/
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
#include "BitArray.h"
#include <iostream>
BitArray::BitArray(void)
{
bits = std::vector<char>();
reset();
}
BitArray::BitArray(size_t initSize)
{
bits = std::vector<char>(initSize);
}
bool BitArray::get(int index)
{
int charIndex = index/8;
int bitIndex = index % 8;
if(charIndex >= bits.size())
return false;
return (bits.at(charIndex) & (1 << bitIndex));
}
void BitArray::set(size_t index)
{
if(index > bits.capacity())
{
std::cout << "Resizing... " << index/8 << std::endl;
bits.resize(index, 0);
}
int charIndex = index/8;
int bitIndex = index % 8;
bits[charIndex] = bits[charIndex] | 1 << bitIndex;
}
void BitArray::reset()
{
for (auto it = bits.begin(); it != bits.end(); it++)
{
*it = 0;
}
}
//Doesn't print the actual bits, only the integer representation
void BitArray::print()
{
for (auto it = bits.begin(); it != bits.end(); it++)
{
std::cout << int(*it) << std::endl;
}
std::cout << std::endl;
}
BitArray::~BitArray(void)
{
}