Logical operators are used in programing and circuitry to transform inputs. Logical operators always deal with true and false: 1 or 0. Thus they always operate on the binary representation of a number or value. In circuitry this is usually a HIGH or LOW voltage.
There are two types of logical operators:
The symbols shown below are common (and correct for C based languages), however not globally true.
Operator | Bitwise Symbol | Logical Symbol |
---|---|---|
AND | & | && |
OR | | | | |
NOT | | ! |
XOR | N/A |
See: Bitwise Operators for more detailed information about the bitwise versions of these operations.
The AND
operator returns true
only if both operands are true
. Otherwise, it returns false
.
Operand 1 | Operand 2 | Result |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
The OR
operator returns true
if at least one operand is true
. Otherwise, it returns false
.
Operand 1 | Operand 2 | Result |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
This flips the bits of the input. true
becomes false
, and false
becomes true
.
Operand | Result |
---|---|
true | false |
false | true |
The XOR
operator, also known as the exclusive OR operator, returns true
if only one operand is true
, but not both. Otherwise, it returns false
.
Operand 1 | Operand 2 | Result |
---|---|---|
true | true | false |
true | false | true |
false | true | true |
false | false | false |