Skip to content

Commit 2dd0a81

Browse files
committed
Add some documentation for BitStream
1 parent 617a63e commit 2dd0a81

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ const crc = bstream.readBits(12); // read in 12 bits as CRC, advancing the point
134134
const flagbits = bstream.peekBits(6); // look ahead at next 6 bits, but do not advance the pointer
135135
```
136136

137-
<!-- TODO(bitjs): Provide better documentation -->
137+
More explanation and examples are located on [the API page](./docs/bitjs.io.md).
138138

139139
## Reference
140140

docs/bitjs.io.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# bitjs.io
2+
3+
This package includes stream objects for reading and writing binary data at the bit and byte level:
4+
BitStream, ByteStream.
5+
6+
## BitStream
7+
8+
A bit stream is a way to read a variable number of bits from a series of bytes. This useful for
9+
parsing certain protocols (for example pkzip or rar algorithm). Note that the order of reading
10+
bits can go from least-to-most significant bit, or the reverse.
11+
12+
### Least-to-Most Direction
13+
14+
![BitStream reading from least-to-most significant bit](bitstream-ltm.png)
15+
16+
```javascript
17+
const bstream = new BitStream(ab, false /* mtl */);
18+
bstream.readBits(6); // (blue) 0b001011 = 11
19+
bstream.readBits(5); // (red) 0b11001 = 25
20+
bstream.readBits(8); // (green) 0b10000010 = 130
21+
```
22+
23+
### Most-to-Least Direction
24+
25+
![BitStream reading from most-to-least significant bit](bitstream-mtl.png)
26+
27+
```javascript
28+
const bstream = new BitStream(ab, true /* mtl */);
29+
bstream.readBits(6); // (blue) 0b010010 = 18
30+
bstream.readBits(5); // (red) 0b11000 = 24
31+
bstream.readBits(8); // (green) 0b10110100 = 180
32+
```

docs/bitstream-ltm.png

29.6 KB
Loading

docs/bitstream-mtl.png

29 KB
Loading

0 commit comments

Comments
 (0)