-
Notifications
You must be signed in to change notification settings - Fork 3
/
bitarray.js
38 lines (33 loc) · 1.43 KB
/
bitarray.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
function BitArray(size, value){
if(value === undefined) value = 0;
this.size = size;
this.field = new Array(~~((size - 1) / BitArray.ELEMENT_WIDTH) + 1);
for(var i = 0; i < this.field.length; i++)
this.field[i] = value == 0 ? 0 : (value << BitArray.ELEMENT_WIDTH) - 1;
}
// will fail for values higher than 30
BitArray.ELEMENT_WIDTH = 24;
// Set a bit (1/0)
BitArray.prototype.set = function(position, value){
if (value == 1)
this.field[~~(position/BitArray.ELEMENT_WIDTH)] |= 1 << (position % BitArray.ELEMENT_WIDTH);
else if(this.field[~~(position/BitArray.ELEMENT_WIDTH)] & 1 << (position % BitArray.ELEMENT_WIDTH))
this.field[~~(position/BitArray.ELEMENT_WIDTH)] ^= 1 << (position % BitArray.ELEMENT_WIDTH);
}
// Read a bit (1/0)
BitArray.prototype.get = function(position){
return (this.field[~~(position/BitArray.ELEMENT_WIDTH)] & 1 << (position % BitArray.ELEMENT_WIDTH)) > 0 ? 1 : 0;
}
// Iterate over each bit
BitArray.prototype.each = function(method){
for (var index = 0; index < this.size; index++) method(this.get(index), index);
}
// Returns the field as a string like "0101010100111100," etc.
BitArray.prototype.toString = function(){
var string = this.field.map(function(ea){
var binary = ea.toString(2);
binary = (new Array(BitArray.ELEMENT_WIDTH - binary.length + 1).join('0')) + binary;
return binary;
}).reverse().join('');
return string.split('').reverse().join('').slice(0,this.size);
}