-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (50 loc) · 1.42 KB
/
index.js
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
const BITS_PER_BYTE = 8;
function BitStream(bits) {
this.bits = bits;
}
BitStream.fromByteArray = function(array) {
var bits = [ ];
var capacity = array.length * BITS_PER_BYTE;
for (var i = 0; i < capacity; i++) {
var byteIndex = Math.floor(i / BITS_PER_BYTE);
var bitIndex = i % BITS_PER_BYTE;
bits.push((array[byteIndex] >> bitIndex) & 1);
}
return new BitStream(bits);
};
BitStream.fromBuffer = function(buffer) {
return BitStream.fromByteArray(buffer.toJSON());
};
BitStream.prototype.take = function(count) {
return new BitStream(this.bits.splice(0, count));
};
BitStream.prototype.toAscii = function() {
return this.toByteArray().map(function(byte) {
return String.fromCharCode(byte);
}).join("");
};
BitStream.prototype.toString = function() {
return this.bits.reverse().join("");
};
BitStream.prototype.toByteArray = function() {
var bytes = [ ];
while (this.bits.length > 0) {
bytes.push(this.take(BITS_PER_BYTE).toUnsigned());
}
return bytes;
};
BitStream.prototype.toSigned = function() {
var size = this.bits.length - 1;
var n = this.take(size).toUnsigned();
if (this.take(1).toUnsigned() > 0) {
n -= Math.pow(2, size);
}
return n;
};
BitStream.prototype.toUnsigned = function() {
return parseInt(this.toString(), 2);
};
BitStream.prototype.reverse = function() {
return BitStream.fromByteArray(this.toByteArray().reverse());
};
module.exports = BitStream;