-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitStream.cs
67 lines (54 loc) · 1.37 KB
/
BitStream.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Merthsoft.TokenIDE {
class BitStream {
private List<byte> data;
private int position;
public bool AtEnd { get { return position >= data.Count * 8; } }
public byte[] Data { get { return data.ToArray(); } }
public int Length { get { return data.Count * 8; } }
public BitStream() {
data = new List<byte>();
data.Add(0);
position = 0;
}
public BitStream(byte[] data, int position) {
this.data = data.ToList();
this.position = position;
}
public UInt64 Read(int numBits) {
UInt64 ret = 0;
for (int i = 0; i < numBits; i++) {
ret <<= 1;
ret += Read();
}
return ret;
}
public byte Read() {
int dataPosition = position / 8;
int bitPosition = 7-position % 8;
position++;
return (byte)((data[dataPosition] >> bitPosition) & 1);
}
public void Write(UInt64 value, int numBits) {
for (int i = numBits-1; i >= 0; i--) {
Write(((value >> i) & 1) == 1);
}
}
public void Write(bool bit) {
int dataPosition = position / 8;
int bitPosition = 7 - position % 8;
position++;
while (dataPosition >= data.Count) {
data.Add(0);
}
int val = bit ? 1 : 0;
byte mask = ((byte)(~(1 << bitPosition)));
byte b = (byte)(val << bitPosition);
data[dataPosition] &= mask;
data[dataPosition] += b;
}
}
}