A utility for bitwise operations that supports both number
and bigint
types in JavaScript/TypeScript.
npm install bitwise.js --save
import { bitwise } from "bitwise.js";
console.log(bitwise.and(5, 3n)); // => 1n (5 & 3n)
console.log(bitwise.or(2.7, 3)); // => 3 (2 | 3)
console.log(bitwise.xor(5, 3)); // => 6 (5 ^ 3)
console.log(bitwise.not(5)); // => -6 (~5)
console.log(bitwise.leftShift(3.9, 2)); // => 12 (3 << 2)
console.log(bitwise.rightShift(5n, 1)); // => 2n (5n >> 1)
-
and(a, b, prefer = 'bigint')
: Performs bitwise AND ona
andb
. -
or(a, b, prefer = 'bigint')
: Performs bitwise OR ona
andb
. -
xor(a, b, prefer = 'bigint')
: Performs bitwise XOR ona
andb
. -
not(a)
: Performs bitwise NOT ona
. -
leftShift(a, bits = 1)
: Performs a left shift operation ona
. -
rightShift(a, bits = 1)
: Performs a right shift operation ona
. -
zeroFillRightShift(a, bits = 1)
: Performs an unsigned right shift ona
(only works with numbers).
The Anti 996 License