-
Notifications
You must be signed in to change notification settings - Fork 0
/
BITBUFOR.h
88 lines (85 loc) · 1.65 KB
/
BITBUFOR.h
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
#ifndef BITBUFOR_H
#define BITBUFOR_H
#include "system.h"
struct BITBUFOR{
Byte *data;
int size;
Byte bitIndex;
int pos;
BITBUFOR(int _size)
{
size=_size;
data=0;
bitIndex=0;
pos=0;
data=new Byte[_size];
memset(data,0,_size);
}
BITBUFOR()
{
size=0;
data=0;
pos=0;
bitIndex=0;
}
inline void addBit(Byte bit){
if(bit)
{
data[pos]|=(1<<(7-bitIndex));///7-bitIndex lub bitIndex!!!
}
bitIndex++;
if(bitIndex==8){pos++;bitIndex=0;}
}
inline void addByte(Byte byte){
for(int i=0;i<8;i++)
{
Byte x=(byte & (1 << i)) != 0;
addBit(x);
}
}
inline Byte getBit(){
Byte bit=(data[pos] & (1 << (7-bitIndex))) != 0;///7-bitIndex lub bitIndex!!!
bitIndex++;
if(bitIndex==8){pos++;bitIndex=0;}
return bit;
}
inline Byte getByte(){
Byte x=0;
for(int i=0;i<8;i++)
{
Byte b=getBit();
if(b)
{
x|=(1<<i);
}
}
return x;
}
/*Byte getNBytes(Byte n)
{
short s;
memcpy(&s,&data[pos],2);
s=s>>(16-bitIndex-n);
short t=-128;
t=t>>(16-n);
return s&t;
bitIndex+=n;
if(bitIndex>=8) bitIndex-=8;
}*/
~BITBUFOR()
{
delete[]data;
}
void show(int _length)
{
for(int i=0;i<_length;i++)
{
printf("%4d",data[i]);
}
}
void clear()
{
memset(data,0,size);
}
};
#endif // BITBUFOR_H