Binary Logical Operators
Logical Operations:
- AND (&): Sets each bit of the result to 1 if both corresponding bits of the inputs are 1.
- OR (|): Sets each bit of the result to 1 if at least one corresponding bit of the inputs is 1.
- XOR (^): Sets each bit of the result to 1 if the corresponding bits of the inputs are different.
- NOT (~): Inverts each bit of the input, changing 1s to 0s and vice versa. Note that only the first input is used for the NOT operation.
And Example
The AND operation compares each pair of corresponding bits in the two binary numbers. The result has a 1 in a particular bit position only if both input bits in that position are 1; otherwise, the result has a 0.
Input 1: 10101010
Input 2: 11001100
—————–
Result: 10001000
In this example, only the third and seventh bits have 1’s in both inputs, so the AND operation results in “10001000.”
Or Example
The OR operation compares each pair of corresponding bits in the two binary numbers. The result has a 1 in a particular bit position if at least one of the input bits in that position is 1.
Input 1: 10101010
Input 2: 11001100
—————–
Result: 11101110
In this example, bits with 1s in either input contribute to the OR operation result, producing “11101110.”
XOR Example
The XOR (exclusive OR) operation compares each pair of corresponding bits in the two binary numbers. The result has a 1 in a particular bit position only if the input bits in that position are different.
Input 1: 10101010
Input 2: 11001100
—————–
Result: 01100110
In this example, only the second, fourth, sixth, and eighth bits have different values in the inputs, resulting in “01100110.”
Not Example
The NOT operation inverts each bit of the input.
Input 1: 10101010
—————–
Result: 01010101
In this example, each 1 in the original input becomes a 0, and each 0 becomes a 1, resulting in “01010101.”