Binary logic is a very simple concept, often associated with a lot of unnecessarily complicated
explanations which can make it seem far more difficult to understand than it
really is.
So here is a nice click-and-play demo;
Yellow represents a "1", or "on".
Grey represents a "0", or "off".
Click on the inputs on the left, to set their output on the right.
The Logical OR gate will emit a "1" if either (or both) of its inputs are "1".
Input 1
Input 2
Output
0
0
0
0
1
1
1
0
1
1
1
1
OR is represented with the symbol above, or in code, most often as "A || B", or sometimes as "A | B".
Logical AND Gate
The Logical AND gate will only emit a "1" if both its inputs are "1".
Input 1
Input 2
Output
0
0
0
0
1
0
1
0
0
1
1
1
Like OR, AND is represented with the symbol above, or in code, most often as "A && B", or "A & B", though the latter can also (in the UNIX shell) be interpreted as "run A in the background, and immediately start B".
Logical NOT Gate
The Logical NOT gate will reverse its input.
Input
Output
0
1
1
0
In most languages, the "!" symbol represents NOT. So, "a = !b" assigns the opposite of b to a.
Logical NAND Gate
The Logical NAND gate is an AND gate followed by a NOT gate. It will emit a 1 unless its inputs are both 1. The circle after the AND symbol indicates the NOT being performed after the AND.
Input 1
Input 2
Output
0
0
1
0
1
1
1
0
1
1
1
0
Logical NOR Gate
The Logical NOR Gate is an OR gate followed by a NOT gate. It will emit a "1" only if both its inputs are "0". The circle after the OR symbol indicates the NOT being performed after the OR.
Input 1
Input 2
Output
0
0
1
0
1
0
1
0
0
1
1
0
Logical XOR Gate
The Logical XOR (eXclusive OR) gate will emit a "1" if neither of its inputs are the same.
Input 1
Input 2
Output
0
0
0
0
1
1
1
0
1
1
1
0
Because of its transparent reversibility, XOR is often used in cryptography. You can XOR the plaintext with a one-time-pad to produce a cyphertext. The recipient can XOR the cyphertext with the one-time-pad to retrieve the plaintext.
For example: 0110 0101 0111 1011 (plaintext - 25,979, the original message) 1010 1101 1101 1001 (onetimepad - 44,505, a shared secret) ---- ---- ---- ---- XOR 1100 1000 1010 0010 (cyphertext - 51,362, the encoded message)
So the original plaintext is retrieved from the encoded message and the shared secret.
Logical XNOR Gate
The Logical XNOR Gate is an XOR gate followed by a NOT gate. It will emit a "1" only if both its inputs are the same. The circle after the XOR symbol indicates the NOT being performed after the XOR.