Bitand symbol (&)

This command performs a binary comparison of two integers, where each bit of the result is the binary AND of the corresponding bits of the inputs, and then converts this binary result to an integer.

Syntax

number_1 & number_2

Number_1 and number_2 must be integers.

Return value

This command returns a number.

Notes

How Bitand works:

    1. Bitand converts the integers number_1 and number_2 into binary numbers.

    2. It creates a new binary number by placing a 1 in every position where the binary values of number_1 and number_2 both contain a 1, and by placing a 0 in every other position.

    3. Bitand then converts the new binary number to an integer and returns this integer value.

Bitand is a convenient way to determine whether a smaller value is "contained" in a larger value. For example, it can be used with RTF_STATUS to determine whether a particular real-time failure has occurred.

Alternately, the Bitand can be used to perform multiple logical tests in an IF statement. In the case of more than two conditionals being tested, the conditionals are tested from left to right. Thus, you must use parentheses to control the precedence.

Examples of precedence:

true | false & false = false [this evaluates to (true or false) and false which equals false]

true | (false & false) = true [this evaluates to true or (false and false) which equals true]

Note: When using Bitand or Bitor in the logical_test, use caution with comparative commands, which do not always return 1 for "True" and 0 for "False":

  • The Less Than symbol (<) compares two numbers as Number 1 < Number 2 and returns the numeric value of Number 2 - Number 1 .
    If Number 1 is less than Number 2, this return value will be greater than 0 - but it won't necessarily equal 1.

  • The Greater Than symbol (>) compares two numbers as Number 1 > Number 2 and returns the numeric value of Number 1 - Number 2 .
    If Number 1 is greater than Number 2, this return value will be greater than 0 - but it won't necessarily equal 1.

  • The INSTR command returns the starting position of the text string you are looking for.
    If the text string is found, this return value will be greater than 0 - but it won't necessarily equal 1.
    This command does not indicate whether the block of text contains only the text string you are looking for - the text block may contain additional characters.

  • The STRCOMP command compares two strings as STRCOMP "String 1", "String 2" .
    If the two strings match exactly, the return value is 0.
    If String 1 comes before String 2 when sorted in order alphanumerically, the return value is -1.
    If String 2 comes before String 1 when sorted in order alphanumerically, the return value is 1.

One exception is the Equals symbol (=), which always returns 1 for "True" (numbers are equal) and 0 for "False" (numbers not equal).

See Examples below for methods to include comparative commands >, <, and = with Bitand or Bitor in the logical_test.  For string comparisons, you can substitute a STRCOMP command for one of the numbers in an Equals (=) comparison, or you can substitute an INSTR command for one of the numbers in a Less than (<) or Greater than (>) comparison.

Examples

16 & 48

The binary comparison of the integers 16 and 48 looks like this:

 

bit positions of binary number

64

32

16

8

4

2

1

integer 16 =

0

0

1

0

0

0

0

integer 48 =

0

1

1

0

0

0

0

result of '16 & 48' =

0

0

1

0

0

0

0

The Bitand command then converts this new binary value (0010000) to its integer value (16), so this formula returns the number 16.

16 & 32

The binary comparison of the integers 16 and 32 looks like this:

 

bit positions of binary number

64

32

16

8

4

2

1

integer 16 =

0

0

1

0

0

0

0

integer 32 =

0

1

0

0

0

0

0

result of '16 & 32' =

0

0

0

0

0

0

0

The Bitand command then converts this new binary value (0000000) to its integer value (0), so this formula returns the number 0.

IF ([A1] = 5) & ([B1] = 10), "Department A", "Department B"

If cell [A1] equals 5 And cell [B1] equals 10, return the string value "Department A". Otherwise, return the string value "Department B".

IF [A1] > 5,  MyNVar "Comparison1" = 1, MyNVar "Comparison1" = 0 :

IF [B1] < 10, MyNVar "Comparison2" = 1, MyNVar "Comparison2" = 0 :

IF MyNVar "Comparison1" & MyNVar "Comparison2", "Department A", "Department B"

If cell [A1] is greater than 5 And cell [B1] is less than 10, return the string value "Department A". Otherwise, return the string value "Department B".

IF (IF [A1] > 5, 1, 0) & (IF [B1] < 10, 1, 0), "Department A", "Department B"

If cell [A1] is greater than 5 And cell [B1] is less than 10, return the string value "Department A". Otherwise, return the string value "Department B".