forked from peterolson/BigInteger.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testbit_example.html
72 lines (60 loc) · 2.77 KB
/
testbit_example.html
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
<script language="JavaScript" type="text/javascript" src="BigInteger.js"></script>
<script>
function bit_test(value, bit)
{
//check value and bit
if(!bigInt.isInstance(value)){throw new Error("Function bit_test: value not a bigInteger");}
if(typeof bit==='undefined'){var bit = 1;}
if(!Number.isInteger(bit) && bigInt.isInstance(bit)){bit = bit.toJSNumber();}
else if(!Number.isInteger(bit) && !bigInt.isInstance(bit)){throw new Error("Function bit_test: bit not a nubmer");}
return ((value.shiftRight(bit-1).value[0] || value.shiftRight(bit-1).value) & 1) //=== 1; //uncomment this if you need true or false
}
//test
var test = bigInt.randBetween('1', '1e32'); //rand int
var bit_number = bigInt.randBetween(bigInt('1'), test.bitLength()); //rand bit
var test_bin = test.toString(2); //binary string
//show this bit
var bits_before_bit = test_bin.substring(0, test_bin.length-bit_number.toJSNumber());
var bit_in_string = test_bin.substring(test_bin.length-bit_number.toJSNumber(), test_bin.length-bit_number.toJSNumber()+1);
var bits_after_bit = test_bin.substring(test_bin.length-bit_number.toJSNumber()+1, test_bin.length);
//new string with big bit
var test_bin = bits_before_bit+'<font size="6"><b>'+bit_in_string+'</b></font>'+bits_after_bit;
var bit_test = bit_test(test, bit_number);
document.write(
'<br><br>'
,'Generated test number: ', test.toString() //number
,'<br>test -> to bin: ', test_bin, ' (',test.bitLength().toString(), 'bits)' //bin string with bitlength
,'<br>'+bit_number.toString()+'-th bit value: ', bit_test//n-th bit
,'<br>(bit_test === parseInt(bit_in_string)): <b><big>', (bit_test === parseInt(bit_in_string)),'</big></b>'
);
bigInt.prototype.testBit = function (index) {
if(!bigInt.isInstance(this)){throw new Error("Function test: this not a bigInteger");}
if(typeof index==='undefined'){var index = 1;}
if(!Number.isInteger(index) && bigInt.isInstance(index)){index = index.toJSNumber();}
else if(!Number.isInteger(index) && !bigInt.isInstance(index)){throw new Error("Function bit_test: index not a nubmer");}
return ((this.shiftRight(index).value[0] || this.shiftRight(index).value) & 1) === 1; //uncomment this if you need true or false
};
function verify(number) {
var result = "";
var index = number.bitLength();
while(index--)
result += number.testBit(index) ? "1" : "0";
var control = number.toString(2);
document.write(
"<br><br><br>number: ", number.toString()
,"<br>control: ", control
,"<br>result: ", result
,"<br><b><big>*** ", control == result ? "PASS" : "FAIL", " ***</big></b>"
,"<br><b><big>(", number.bitLength()," bits)</big></b>"
);
}
document.write("<br><hr>Another test");
/*
Small integer passes
*/
verify(bigInt.randBetween("1", "1e15"));
/*
True big integer passes
*/
verify(bigInt.randBetween("1", "1e70"));
</script>