2 - 22 2 - 22
MELSEC-Q
2 THE BASICS OF AD51H-BASIC
Note
• Operations involving logical operators are performed after converting the numeric
constant or numeric variable to an integer within the range from –32768 to
+32767 (expressed as two’s-compliment number if negative).
If the value exceeds the range from –32768 to +32767, an Overflow error will
occur.
• Logical operations are performed bitwise between two integers. The
corresponding bits in the two integers determine each resulting bit.
For example, an AND operator can be used to “mask” all bits except a certain bit.
Then an OR operator can be used to combine the contents of the two bytes. This
allows the creation of a specific binary number.
The following describes how this is done using logic operators.
Example
63 AND 16 • • • • • • • • • • 16
63 is 111111 in binary code. 16 is 10000 in binary code.
Therefore, 63 AND 16 = 16.
63 • • • 0000 0000 0011 1111
AND 16 • • • 0000 0000 0001 0000
0000 0000 0001 0000 • • • 16
15 AND 14 • • • • • • • • • • 14
15 is 1111 in binary code. 14 is 1110 in binary code.
Therefore, 15 AND 14 = 14.
-1 AND 8
• • • • • • • • • • 8
-1 is 1111111111111111 in binary. 8 is 1000 in binary.
Therefore, –1 AND 8 = 8.
4 OR 0
• • • • • • • • • • 6
4 is 100 in binary. 2 is 10 in binary.
Therefore, 4 OR 2 = 6.
4 • • • 0000 0000 0000 0100
OR 2 • • • 0000 0000 0000 0010
0000 0000 0000 0110 • • • 6
10 OR 10
• • • • • • • • • • 10
10 is 1010 in binary code. Therefore, 1010 OR 1010 = 1010 (in
other words, 10.)
-1 OR -2
• • • • • • • • • • -1
-1 is 1111111111111111 in binary code. –2 is 1111111111111110
in binary code. Therefore, -1 OR -2 = -1.
NOT X
• • • • • • • • • • -(X+1)
Two’s-compliment of an arbitrary integer is the value obtained by
reversing all the bits of the integer and adding 1.
3 • • • 0000 0000 0000 0011
Bit reversal • • • 1111 1111 1111 1100
Add 1 • • • 1
–3 • • • 1111 1111 1111 1101 • • • two’s-compliment
numbers