-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
40 lines (34 loc) · 1.11 KB
/
index.js
File metadata and controls
40 lines (34 loc) · 1.11 KB
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
const bigIntSupport = typeof BigInt !== 'undefined';
function divide(bit) {
const bitString = bit.toString(2).padStart(64, '0');
return [
parseInt(bitString.slice(0, 32), 2),
parseInt(bitString.slice(-32), 2)
]
}
function pad(bit) {
return bit.toString(2).padStart(32, '0')
}
export function and(a, b){
if(bigIntSupport) return Number(BigInt(a) & BigInt(b));
const _a = divide(a);
const _b = divide(b);
return parseInt(pad((_a[0] & _b[0]) >>> 0) + pad((_a[1] & _b[1]) >>> 0), 2)
}
export function or(a, b){
if(bigIntSupport) return Number(BigInt(a) | BigInt(b));
const _a = divide(a);
const _b = divide(b);
return parseInt(pad((_a[0] | _b[0]) >>> 0) + pad((_a[1] | _b[1]) >>> 0), 2)
}
export function not(a){
if(bigIntSupport) return Number(~BigInt(a));
const _a = divide(a);
return parseInt(pad((~_a[0]) >>> 0) + pad((~_a[1]) >>> 0), 2)
}
export function xor(a, b){
if(bigIntSupport) return Number(BigInt(a) ^ BigInt(b));
const _a = divide(a);
const _b = divide(b);
return parseInt(pad((_a[0] ^ _b[0]) >>> 0) + pad((_a[1] ^ _b[1]) >>> 0), 2)
}