-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitstream.cpp
More file actions
30 lines (26 loc) · 782 Bytes
/
bitstream.cpp
File metadata and controls
30 lines (26 loc) · 782 Bytes
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
#include <iostream>
#include <cstring>
using namespace std;
int main() {
string bitstream = "111000101000100100010001";
unsigned char buffer[100];
memset(buffer,0, 100);
int ind = 0;
int cont_bits = 0;
for(int i = 0; i < bitstream.size(); i++) {
buffer[ind] = (buffer[ind] << 1) | ((int)(bitstream[i]-'0')); //desloca um bit para a esquerda para adicionar o próximo bit
cont_bits++;
if(cont_bits == 8) { //verifica se finalizou um byte
cont_bits = 0;
ind++;
}
}
cout << bitstream << endl;
//mostrando os bits a partir do buffer
for(int i = 0; i < ind; i++) {
for(int k = 7; k >= 0; k--) {
cout << (int)((buffer[i] >> k) & 1);
}
}
return 0;
}