-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitstring.cpp
More file actions
111 lines (93 loc) · 1.76 KB
/
bitstring.cpp
File metadata and controls
111 lines (93 loc) · 1.76 KB
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
// basic implementation of bitset using arrays
// might be useful as bitsets cannot be dynamically allocated during runtime
// testing / error handling / documentation PRs will be highly appreciated
#include <iostream>
#include <stdint.h>
#include <bitset>
#include <cstring>
using std::bitset;
using std::cout;
class bitstring
{
private:
uint8_t *bit_array;
int length;
public:
// constructor
bitstring(size_t size)
{
length = size / 8 + 1;
bit_array = new uint8_t(length);
memset(bit_array, 0, length);
}
// destructor
~bitstring()
{
delete[] bit_array;
}
// setting the i'th bit
void set(int i, bool value = true)
{
int index = i / 8;
int n = i % 8;
if (value == true)
bit_array[index] |= 1 << n;
else
bit_array[index] &= !(1 << n);
}
// accessing the i'th bit
int get(int i)
{
int index = i / 8;
int n = i % 8;
int bit = (bit_array[index] >> n) & 1U;
return bit;
}
// toggling the i'th bit
void toggle(int i)
{
int index = i / 8;
int n = i % 8;
bit_array[index] ^= 1 << n;
}
// accessing the i'th bit
int operator[](int i)
{
return get(i);
}
// viewing the bit_array
void show()
{
for (int i = length - 1; i > -1; i--)
{
cout << bitset<8>(bit_array[i]);
}
cout << '\n';
}
// resetting the entire bitstring
void reset()
{
for (int i = 0; i < length; i++)
{
bit_array[i] = 0;
}
}
};
int main()
{
// initializing bit_array of size 20:
bitstring b = bitstring(20);
// setting 4th and 5th bit to true:
b.set(4, true);
// b.set(4) also valid
b.set(3);
// viewing the entire bitstring:
b.show();
// toggling the 4th bit
b.toggle(3);
// accessing the 4th and 5th bit:
cout << "Bit 4 : " << b[3] << ", Bit 5 : " << b[4];
// b.get(4) also valid
// resetting the bitstring:
b.reset();
}