This repository has been archived by the owner on Sep 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitVector.js
125 lines (114 loc) · 3.38 KB
/
BitVector.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* The BitVector class cares about everything you need in your Bitrelated life.
*
* As it is a BitVector, the internal structure is an array of 0s and 1s.
* All operations react like bitvectors, even if future versions of this class
* could use another internal representation.
* The class is designed to be instanciated with several input types, see the
* constructors.
*
* Internal Representation:
* The BitVector is internally represented by an array as follows:
* this.arr = [lsb, lsb+1, ..., msb-1, msb]
* Therefore an input is encoded 'backwards' but can be accessed naturally as
* array index = power of 2.
* Example: 7 (base 10) = 0111 (base 2)
* => var bitvec = new BitVector(7);
* bitvec.get(0) // == 1
* bitvec.get(3) // == 0
*/
/**
* The constructor can be used in several ways.
* If just one parameter is given, the base is guessed. Otherwise see below.
* If no options are given, the length is minimal.
*
* new Bitvector([0,0,1,0]) – interpretes the array's elements as bits,
arr[0] is the MSB, arr[length-1] is the LSB
* new Bitvector("8231") – interpretes the string as a decimal number
* new Bitvector("0x8231") – interpretes the string as a hexacedicmal number
* new Bitvector(5634) – interpretes the integer as a decimal number
*
* Also options = {length: int} can be provided
*/
function BitVector(number, options) {
this.arr = [];
// case 1: the number is given as array
if(number instanceof Array) {
for(var i = 0; i < number.length; i++) {
if(!(number[i] == 0 || number[i] == 1)) {
throw "BitVector(array) expects array of {0,1}"
} else {
number[i] = parseInt(number[i])
}
}
this.arr = number.reverse();
}
// case 2: the number is given as string
// the javascript function parseInt detects the prefix 0x for hexadecimal
if(typeof number == "string") {
this.arr = parseInt(number).toString(2).split('').reverse();
}
// case 3: the number is given as integer
if(typeof number == "number") {
this.arr = number.toString(2).split('').reverse();
}
if(options && options.size) {
// controlled ditch
if(options.size < this.arr.size) {
throw "The value of the BitVector is too big to fit in the requested size."
}
}
}
/**
* Checks for equality of BitVectors.
* If the two BitVectors don't have the same lenght, they are considered unequal.
*/
BitVector.prototype.equals = function(op) {
if(op instanceof BitVector) {
if(op.length() != this.length()) {
return false;
}
for(var i = 0; i < this.length(); i++) {
if(op.get(i) != this.get(i)) {
return false;
}
}
return true;
} else { // op is not a BitVector
return this.equals(new BitVector(op));
}
}
/**
* Returns the size of this BitVector
*/
BitVector.prototype.length = function() {
return this.arr.length;
}
/**
* Returns the BitVector as a string, MSB first, LSB last. i.e.:
* new BitVector([0,1,0,1]) => "0101"
*/
BitVector.prototype.toString = function() {
var res = ""
for(var i = 0; i < this.arr.length; i++) {
res = this.get(i) + res;
}
return res;
}
/**
* Gets element No index from the BitVector.
* get(LSB) = get(0)
*/
BitVector.prototype.get = function(index) {
return this.arr[index];
}
/**
* Returns the integer value of the Bitvector
*/
BitVector.prototype.toInt = function() {
var value = 0;
for(var i = 0; i < this.length(); i++) {
value = value + this.get(i) * Math.pow(2,i)
}
return value;
}