Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.
Logical operators are words that represent a logical relationship between conditions, or that invert a condition. The condition is the result of a relational expression. The operators are listed in order of precedence.
Operator | Action |
! | (NOT) A unary operator that inverts the result of a relational expression. |
&& | (AND) Combines two relational expressions. Each expression must be true for the logical expression to be True. |
^ | (XOR) Combines two relational expressions. Only one expression can be true for the logical expression to be True. If both are true or both are false, the logical expression is False. The XOR operator is also named exclusive OR. |
|| | (OR) Combines two relational expressions. Only one expression needs to be true for the logical expression to be True. The OR operator is also named inclusive OR. |
NOT example
x = 8
If ((x < 10) && ! (x = 5))
Beep
EndIf
Explanation: The result of the logical expression equals True (x is less than 10 AND NOT x is equal to 5, or x is not equal to 5).
AND example
x = 1
y = 2
z = ((x = 1) && (y = 2))
Result: z equals True (the logical AND expression is True because both the relational expressions x = 1 and y = 2 are true).
XOR example
x = 1
y = 2
z = ((x = 0) ^ (y = 2))
Result: z equals True (only one relational expression is true).
z = ((x = 1) ^ (y = 2))
Result: z equals False (both relational expressions are true).
z = ((x = 0) ^ (y = 1))
Result: z equals False (both relational expressions are false).
XOR example in shorthand notation
x = 1
y = 2
If ((x = 0) ^ (y = 2))
Beep
EndIf
Explanation: The result of the logical XOR expression equals True because the relational expression x = 0 is false and the relational expression y = 2 is True.
OR example
x = 1
y = 2
z = ((x = 1) || (y = 5))
Result: z equals True (the logical OR expression is True because the relational expression x = 1 is True).
Logical Expressions
Logical expressions are statements that represent logical operations, or statements that contain two relational expressions joined by a logical operator. The operation result equals True or False.
Given that x equals 10, y equals 5, and z equals 20, the following expressions return True or False in variable w:
w = ((x > y) AND (y < z))
Result: w equals True (both relational expressions are true).
w = ((x < y) AND (y < z))
Result: w equals False (first relational expression is false).
w = !(y > z)
Result: w equals True (5 is not greater than 20).
w = ((x > 5) AND (y < 20) AND (z = 20))
Result: w equals True (all relational expressions are true).
w = (((x < 5) AND (y > 20)) OR (z = 20))
Result: w equals True (z = 20 is true).
w = (((x < 5) AND (y > 20)) OR ! (z = 20))
Result: w equals False (all relational expressions are false).
Bitwise Operators
Bitwise operators are symbols that represent a bitwise operation on two integer operands. The operators are listed in order of precedence.
Operator | Action |
~ | Bitwise unary NOT [complement of 1] toggles a binary value [1 to 0, and 0 to 1]. |
& | Bitwise AND results in 1 if both operand bits are 1, and results in 0 if either of the operand bits are not 1. |
| | Bitwise inclusive OR results in 1 if either operand is 1. Results in 0 if both operands are 0. |
^ | Bitwise exclusive OR (XOR) results in 0 if operands match, and 1 if operands do not match. |
<< | Shift bits left. |
>> | Shift bits right. |
Unary NOT (~) example
Decimal | Binary |
~-15 | (1111111111110001) |
____ | __________________ |
14 | (0000000000001110) |
Explanation: Bitwise unary NOT toggles bits from 1 to 0 and from 0 to 1.
AND (&) examples
x=1000&31
Results: x equals 8
Decimal | Binary |
1000 | (1111101000) |
&31 | (0000011111) |
____ | ____________ |
8 | (0000001000) |
x = 65535 & 535
Result: x equals 535.
Decimal | Binary |
65535 | 1111111111111111 |
&535 | 0000001000010111 |
_____ | ________________ |
535 | 0000001000010111 |
Explanation: Bitwise AND turns off bits (result is 0) if one operand is 0. Bits are left on (result is 1) if both operands are 1.
Inclusive OR (|) examples
x=1000|27
Result: x equals 1019
Decimal | Binary |
1000 | (1111101000) |
|27 | (0000011011) |
____ | ____________ |
1019 | (1111111011) |
x = 65535 | 535
Result: x equals 65,535.
Decimal | Binary |
65535 | 1111111111111111 |
|535 | 0000001000010111 |
_____ | ________________ |
65535 | 1111111111111111 |
Explanation: Bitwise OR turns on bits (result is 1) if one operand is 1. Bits are left off (result is 0) if both operands are 0.
Exclusive OR (^) examples
x=1000^40
Result: x equals 960
Decimal | Binary |
1000 | (1111101000) |
^40 | (0000101000) |
____ | _____________ |
960 | (1111000000) |
x = 65535 ^ 535
Result: x equals 65,000
Decimal | Binary |
65535 | 1111111111111111 |
^ 535 | 0000001000010111 |
_____ | ________________ |
65,000 | 1111110111101000 |
Explanation: Bitwise XOR turns on bits (result is 1) if the operands are different. It turns off bits (result is 0) if the operands are the same.
Shift left (<<) examples
x=500<<1
Result: x equals 1000
Decimal | Binary |
500 | (0111110100) |
<<1 | |
____ | ____________ |
1000 | (1111101000) |
x = 65535 << 1
Result: x equals 131,070.
Decimal | Binary |
65535 | 1111111111111111 |
< < | |
___ | _______________ |
131,070 | 1111111111111110 |
Explanation: Shifts bits one position left, and inserts one 0 bit at the right end of the binary numeral. Multiplies the original value by 2.
Shift right (>>) examples
X=1000>>1
Result: x equals 500
Decimal | Binary |
1000 | (1111101000) |
>>1 | |
____ | ____________ |
500 | (0111110100) |
x = 65535 >> 1
Result: x equals 32,767.
Decimal | Binary |
65535 | 1111111111111111 |
>> | |
____ | ____________ |
32,767 | 0111111111111111 |
Explanation: Shifts bits one position right, and inserts one 0 bit at the left end of the binary numeral. Divides the original value by 2.
Bitwise Expressions
Bitwise expressions are statements that represent bitwise operations, or statements that contain two operands joined by a bitwise operator. The operation result is a numeric value.
Bitwise NOT (~) example
x = ~(-15)
Result: x equals 14 (complement of 1).
x = ~(-15) + 1
Result: x equals 15 (complement of 2).
Bitwise AND (&) example
x = 65535 & 535
Result: x equals 535.
Bitwise inclusive OR (|) example
x = 65535 | 535
Result: x equals 65,535.
Bitwise XOR (^) example
x = 65535 ^ 535
Result: x equals 65,000.
Bitwise shift left (<<) example
x = 65535 << 1
Result: x equals 131,070.
Bitwise shift right (>>) example
x = 65535 >> 1
Result: x equals 32,767.
Article ID: W16016
File Created: 2004:03:30:15:42:16
Last Updated: 2004:03:30:15:42:16