Bitwise operators – Rockwell Automation 1771-DB BASIC MODULE User Manual

Page 117

Advertising
background image

Chapter
Expressions, Variables and Operators

9

9 -7

The BASIC module contains a complete set of bitwise logical operators.
Bitwise operators perform their operations on a bit-by-bit level. Therefore,
BASIC changes the integers in the expressions to HEX/binary. BASIC can
perform bitwise operations on numbers between 0 (0000H) and 65535
(0FFFFH) inclusive. If the argument is outside this range, then the BASIC
module generates an

ERROR: BAD ARGUMENT

message and returns to

Command mode. All decimal places are truncated, not rounded.

Refer to this truth table when performing bitwise operations.

When X is:

and Y is:

X .AND.Y bit set to:

X .OR. Y bit set to:

X .XOR. Y bit set to:

0

0

0

0

0

0

1

0

1

1

1

0

0

1

1

1

1

1

1

0

Note:

0 = false/off

1 = true/on

X = a bit in the first operand

Y = a bit in the second operand

.AND.

Use the .AND. operator to perform a bitwise AND on two expressions.
The .AND. operator compares the bits of the two expressions and sets the
bit to 1 (true) if both bits being compared are set to 1 (true).

>PRINT 3.AND.2

Result:

2

This example performs a bitwise AND on the integers 3 and 2 and prints
the result. First, BASIC converts 3 and 2 to binary:

3 = 0011
2 = 0010

Then the BASIC module compares each digit of the binary number to
determine if both bits are set to a 1. For example, the first digit of both
numbers is 0 (false) so the result is 0.

0011
0010
0010

BASIC converts the resulting binary number back into an integer and
prints the result.

0010 = 2

Bitwise Operators

Advertising