Interactive Logic Concepts
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;
Grey represents a "0", or "off".
Click on the inputs on the left, to set their output on the right.
All seven logical gates are: OR AND NOT NAND NOR XOR XNORLogical OR Gate
The Logical OR gate will emit a "1" if either (or both) of its inputs are "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".
|
|
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.
|
|
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.
|
|
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.
|
|
Logical XOR Gate
The Logical XOR (eXclusive OR) gate will emit a "1" if neither of its inputs are the same.
|
|
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)
Which translates back to:
1100 1000 1010 0010
(cyphertext - 51,362)
1010 1101 1101 1001
(onetimepad - 44,505)
---- ---- ---- ----
XOR
0110 0101 0111 1011
(plaintext - 25,979)
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.
|
|
If you found this interesting or useful, you might want to check out the Shell Scripting Tutorial, Shell CheatSheet (or the PDF Version), and/or my *nix Shell Blog.
Wow! Within 5 days (created Mon 3/9/07), this is already #3 on Google for Binary Logic Gate.