Bitwise Functions (7)

Bitwise logical operations on integers

bitAnd
Bitwise AND operation. Performs the logical AND operation on each pair of the corresponding bits of the two given values by multiplying them. If both bits in the compared position are 1, the bit in the resulting binary representation is 1, otherwise, the result is 0
Syntax
x & y | bitAnd(x | y)
Parameters
NameTypeDescription
xnumber | BigNumber | bigint | Array | MatrixFirst value to and
ynumber | BigNumber | bigint | Array | MatrixSecond value to and
Returns
number | BigNumber | bigint | Array | Matrix — AND of `x` and `y`
Examples
5 & 3
bitAnd(53
131)
[1
12
bitNot
Bitwise NOT operation. Performs a logical negation on each bit of the given value. Bits that are 0 become 1, and those that are 1 become 0.
Syntax
~x | bitNot(x)
Parameters
NameTypeDescription
xnumber | BigNumber | bigint | Array | MatrixValue to not
Returns
number | BigNumber | bigint | Array | Matrix — NOT of `x`
Examples
~1
~2
bitNot([2
-3
4
bitOr
Bitwise OR operation. Performs the logical inclusive OR operation on each pair of corresponding bits of the two given values. The result in each position is 1 if the first bit is 1 or the second bit is 1 or both bits are 1, otherwise, the result is 0.
Syntax
x | y | bitOr(x | y)
Parameters
NameTypeDescription
xnumber | BigNumber | bigint | Array | MatrixFirst value to or
ynumber | BigNumber | bigint | Array | MatrixSecond value to or
Returns
number | BigNumber | bigint | Array | Matrix — OR of `x` and `y`
Examples
5 | 3
bitOr([1
2
3
bitXor
Bitwise XOR operation, exclusive OR. Performs the logical exclusive OR operation on each pair of corresponding bits of the two given values. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1.
Syntax
bitXor(x | y)
Parameters
NameTypeDescription
xnumber | BigNumber | bigint | Array | MatrixFirst value to xor
ynumber | BigNumber | bigint | Array | MatrixSecond value to xor
Returns
number | BigNumber | bigint | Array | Matrix — XOR of `x` and `y`
Examples
bitOr(1
2)
bitXor([2
3
4
leftShift
Bitwise left logical shift of a value x by y number of bits.
Syntax
x << y | leftShift(x | y)
Parameters
NameTypeDescription
xnumber | BigNumber | bigint | Array | MatrixValue to be shifted
ynumber | BigNumber | bigintAmount of shifts
Returns
number | BigNumber | bigint | Array | Matrix — `x` shifted left `y` times
Examples
4 << 1
8 >> 1
rightArithShift
Bitwise right arithmetic shift of a value x by y number of bits.
Syntax
x >> y | rightArithShift(x | y)
Parameters
NameTypeDescription
xnumber | BigNumber | bigint | Array | MatrixValue to be shifted
ynumber | BigNumber | bigintAmount of shifts
Returns
number | BigNumber | bigint | Array | Matrix — `x` zero-filled shifted right `y` times
Examples
8 >> 1
4 << 1
-12 >> 2
rightLogShift
Bitwise right logical shift of a value x by y number of bits.
Syntax
x >>> y | rightLogShift(x | y)
Parameters
NameTypeDescription
xnumber | Array | MatrixValue to be shifted
ynumberAmount of shifts
Returns
number | Array | Matrix — `x` zero-filled shifted right `y` times
Examples
8 >>> 1
4 << 1
-12 >>> 2