Skip to main content

Math: Bitwise Functions

A bitwise function operates on one or more bit patterns or binary numerals at the level of their individual bits. Use a bitwise function to manipulate values for comparisons and calculations. Bitwise functions can only be used with Number data types and will produce an INT64 result.

Important

If double/float values are given as arguments, they are truncated to 64-bit integers. Negative values are treated as two’s complement, 64-bit integer numbers. That is, -16 and -16.9999 are treated as 0xfffffffffffffff0.

BinaryAnd

BinaryAnd(n,m): Returns a binary of (n) and (m). The result is 1 if both n and m are 1, and 0 otherwise. If 0 is equated with False, and 1 with True, the BinaryAnd operation works like a logical And. Note the absence of the decimal point on the result values. The result is an integer, not a double/float value.

Example

BinaryAnd(1,1) returns 1.

BinaryAnd(1,0) returns 0.

BinaryAnd(12,6) returns 4.

BinaryAnd(12.99, 6.99) returns 4.

BinaryAnd(-12, 6.99) returns 4.

BinaryNot

BinaryNot(n): Returns a Binary Not of (n). Numbers are treated as 64-bit, two's complement numbers.

Example

BinaryNot(6) returns -7.

BinaryNot(2) returns -3.

BinaryNot(-1) returns 0.

BinaryNot(0) returns -1.

BinaryOr

BinaryOr(n,m): Returns a Binary Or of (n) and (m).

Example

BinaryOr(6,6) returns 6.

BinaryOr(6,2) returns 6.

BinaryOr(4,2) returns 6.

BinaryOr(12,6) returns 14.

BinaryXOR

BinaryXOr(n,m): Returns a Binary XOr of (n) and (m).

Example

BinaryXOr(6,6) returns 0.

BinaryXOr(6,2) returns 4.

BinaryXOr(6,12) returns 10.

ShiftLeft

ShiftLeft(n,b): Left shifts (n) (as integer) by (b) bits.

ShiftRight

ShiftRight(n,b): Right shifts (n) (as integer) by (b) bits.

For both ShiftLeft and ShiftRight the result is an Int64, so only 64 bits are available.

Example

ShiftLeft(pow(2,62),1) returns -pow(2,63). Shift it left once more and you get zero.

ShiftRight(1,1) returns zero.

ShiftRight(-1,1) returns Int64_Max.