-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryBuffer.cpp
More file actions
138 lines (118 loc) · 3.16 KB
/
Copy pathBinaryBuffer.cpp
File metadata and controls
138 lines (118 loc) · 3.16 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
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
#include "BinaryBuffer.h"
#include <iostream>
void BinaryBuffer::shiftLeft()
{
if(buffer.empty()) buffer.push_back(0);
if(index >= 7)
{
index = 0;
buffer.push_back(0);
}
else
{
index++;
buffer.back() <<= 1;
}
}
void BinaryBuffer::shiftRight()
{
if(buffer.empty()) buffer.push_back(0);
if(index <= 0)
{
buffer.pop_back();
if(buffer.empty()) index = 0;
else index = 7;
}
else
{
index--;
buffer.back() >>= 1;
}
}
BinaryBuffer &BinaryBuffer::operator <<=(size_t amount)
{
for(int i=0; i < amount; i++) shiftLeft();
return *this;
}
BinaryBuffer &BinaryBuffer::operator >>=(size_t amount)
{
for(int i=0; i < amount; i++) shiftRight();
return *this;
}
BinaryBuffer &BinaryBuffer::operator ++()
{
if(buffer.size() == 0) buffer.push_back(0);
buffer.back()++;
return *this;
}
void BinaryBuffer::operator +=(const BinaryBuffer &other)
{
if(other.buffer.size() == 0) return;
//Добавяне байт по байт данните от другия буфер
for(int i=0; i < other.buffer.size() - 1; i++)
{
writeByte(other.buffer[i]);
}
//Добавяне на последните останали битове от другия буфер
unsigned char c = 1 << other.index; //Битова маска, която сочи първият значещ бит от последния байт на другия буфер
for(int j=0; j < other.index; j++)
{
if((other.buffer.back() & c) / c == 1)
{
operator ++();
}
shiftLeft();
c >>= 1;
}
}
void BinaryBuffer::writeByte(const char &byte)
{
if(buffer.size() == 0) buffer.push_back(0);
unsigned char c = 128; //1000 0000 Битова маска за вземане на битовете от ляво на дясно
for(int j=0; j < 8; j++)
{
if((byte & c) / c == 1)
{
operator ++(); //Правим текущият бит 1 ако в другият байт е 1
}
shiftLeft();
c >>= 1; //Местене на маската
}
}
void BinaryBuffer::writeTo(std::ostream &stream)
{
if(buffer.empty()) return;
stream.write((char *)&index, sizeof(char));
for(int i=0; i < buffer.size() - 1;i++)
{
stream.write((char *)&buffer[i], sizeof(char));
}
char lastByte = buffer.back() << (7 - index);
stream.write((char *)&lastByte, sizeof(char));
}
void BinaryBuffer::print() const
{
if(buffer.size() == 0) return;
//Принтиране на всички битове разделени по 8
for(int i=0; i < buffer.size() - 1; i++)
{
unsigned char c = 128;
for(int j=0; j < 8 ;j++)
{
std::cout << (buffer[i] & c) / c;
c >>= 1;
}
std::cout << " ";
}
//Принтиране на последният бит
unsigned char c = 1 << index;
for(int j=0; j < index; j++)
{
std::cout << (buffer.back() & c) / c;
c >>= 1;
}
}
int BinaryBuffer::numBits()
{
return (buffer.size() - 1) * 8 + index;
}