# Logic

Logic functions provide Boolean algebra and conditional testing capabilities to the In-Sight spreadsheet. All bitwise functions perform the corresponding logical operation on each bit position in the binary representation of the arguments and return the resulting binary number in decimal form. For example, bit 0 of Value0 is compared to bit 0 of Value1, bit 1 of Value0 is compared to bit 1 of Value1, etc.

Tip: Bitwise functions are particularly useful with the MultiStatus function, when combining the results of multiple true/false conditions into a GUI display or discrete output.

Operator | Symbol
--- | ---
NOT | !
AND | &&
OR | ||
Equivalent to | ==
Not equal to | <> (or !=)

For all Logic functions:

- FALSE = 0
- TRUE = any non-zero result

Note: Boolean functions will treat any argument referencing an empty cell or a cell containing an empty string as FALSE. Arguments referencing any non-empty string (even the string "0") are treated as TRUE.

Function | Description
And(Value1, Value2, ...) | Returns 1 if all values are non-zero. (Logical AND the result is TRUE when both values are TRUE.) Examples: And(45, -77) returns 1. And(65534, 0) returns 0. And(-3, -2, -1, 1, 2, 3) returns 1. And(-1, 0, 1) returns 0.
BitAnd(Value1, Value2, ...) | Returns the bitwise AND of values. Note: Processes and returns 32-bit signed integer values. Examples: BitAnd(45, 77) returns 13. 45 = 0000 0000 0000 0000 0000 0000 0010 1101 in binary, 77 = 0000 0000 0000 0000 0000 0000 0100 1101 in binary. Individually comparing each common bit in these two 16-bit binary numbers using the And operation yields 0000 0000 0000 0000 0000 0000 0000 1101, which = 13 in decimal.
BitNot(Value) | Returns the bitwise inverse of Value. Note: Processes and returns 32-bit signed integer values. Examples: BitNot(45) returns -46. 45 = 0000 0000 0000 0000 0000 0000 0010 1101 in binary. Inverting each 0 to a 1 and each 1 to a 0 yields 1111 1111 1111 1111 1111 1111 1101 0010 in binary, which is -46 in decimal.
BitOr(Value1, Value2, ...) | Returns the bitwise OR of values. Note: Processes and returns 32-bit signed integer values. Examples: BitOr(45, 77) returns 109. 45 = 0000 0000 0000 0000 0000 0000 0010 1101 in binary, 77 = 0000 0000 0000 0000 0000 0000 0100 1101 in binary. Individually comparing each common bit in these two 16-bit binary numbers using the Or operation yields 0000 0000 0000 0000 0000 0000 0110 1101, which = 109 in decimal.
BitXor(Value1, Value2, ...) | Returns the bitwise XOR (exclusive OR) of Values. (Logical XOR the result is TRUE if one – and only one – of the bits is TRUE. The result is FALSE if 2 or more bits are equal.) Note: Processes and returns 32-bit signed integer values. Examples: BitXor (45, 77) returns 96. 45 = 0000 0000 0000 0000 0000 0000 0010 1101 in binary, 77 = 0000 0000 0000 0000 0000 0000 0100 1101 in binary. 0110 0000 are the only non-common bits between 45 and 77, as demonstrated below.
45 | 77 | XOR per bit | XOR bit
0 | 0 | two 0s ≥ XOR FALSE | 0 (FALSE)
0 | 1 | only one 1 ≥ XOR TRUE | 1 (TRUE)
1 | 0 | only one 1 ≥ XOR TRUE | 1 (TRUE)
0 | 0 | two 0s ≥ XOR FALSE | 0 (FALSE)
1 | 1 | two 1s ≥ XOR FALSE | 0 (FALSE)
1 | 1 | two 1s ≥ XOR FALSE | 0 (FALSE)
0 | 0 | two 0s ≥ XOR FALSE | 0 (FALSE)
1 | 1 | two 0s≥ XOR FALSE | 0 (FALSE)
0110 0000 = 96 in decimal.
If(Cond, Value1, Value2) | Returns Value1 if Cond is non-zero; otherwise, returns Value2. Note: If the condition or the arguments reference empty cells, they are interpreted as a zero (FALSE).
InRange(Value, Start, End) | Returns TRUE if Min(Start,End) ≤ Value ≤ Max(Start,End).
InTolerance(Actual, Expected, Tolerance %, Margin) | Determines if the Actual value is within the relative Tolerance % of the Expected value, provided that the difference between the Actual and Expected values is above the Margin. If the value of the calculated percentage difference is larger than the value of the Tolerance % or the Margin arguments, then the function returns 0. Otherwise, it returns 1. The calculated values use the Expected and Actual values provided.
Not(Value) | Returns 0 if Value is non-zero, or 1 if Value is 0. (Logical Inverse) Examples: Not(-12345) returns 0. Not(3.14) returns 0. Not(2-1) returns 0. Not(1-1) returns 1.
OneShot(Value) | Returns true (1) if the latest input value is true (non-zero) and the previous input value was false (0). In all other cases, the function returns false (0). Examples: OneShot(A1) refers to the A1 cell containing a boolean function. On first execution, the A1 cell returns 0. OneShot(A1) returns 0. On second execution, the A1 cell returns 1. OneShot(A1) returns 1. OneShot(B2) refers to the B2 cell containing a function that returns a wider range of values. On first execution, the B2 cell returns 12. OneShot(B2) returns 1. On second execution, the B2 cell returns 0. OneShot(B2) returns 0. Note: Depending on trigger speed, OneShot function results can pulse very rapidly. This can mean that the output value does not visibly update in the display.
Or(Value1, Value2, ...) | Returns 0 if all values are zero; otherwise, returns 1. (Logical OR the result is TRUE if any of the Values are TRUE.) Examples: Or(45, -77) returns 1. Or(-1, 0, 1) returns 1. Or(0, 1-1) returns 0.