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;

On Yellow represents a "1", or "on".
Off 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 XNOR

Logical OR Gate

The Logical OR gate will emit a "1" if either (or both) of its inputs are "1".

click me!
Off OR Gate
 On
On
Input 1Input 2Output
000
011
101
111

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".

click me!
On AND Gate
 Off
Off
Input 1Input 2Output
000
010
100
111

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.

click me!
On NOT Gate Off
InputOutput
01
10

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.

click me!
On NAND Gate
 On
Off
Input 1Input 2Output
001
011
101
110

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.

click me!
Off NOR Gate
 Off
On
Input 1Input 2Output
001
010
100
110

Logical XOR Gate

The Logical XOR (eXclusive OR) gate will emit a "1" if neither of its inputs are the same.

click me!
Off XOR Gate
 On
On
Input 1Input 2Output
000
011
101
110

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.

click me!
Off XNOR Gate
 Off
On
Input 1Input 2Output
001
010
100
111


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.

Logic Gates
Share on Twitter Share on Facebook Share on LinkedIn Share on Identi.ca Share on StumbleUpon

Get the Book:

My Shell Scripting Book:
    Shell Scripting, Expert Recipes for Linux, Bash and more
is available online and from all good booksellers.

Buy my 600-page Shell Scripting Book...

From Amazon USA:

Or from other retailers

Facebook:

You can also join a Shell Scripting community on Facebook:

Get the Tutorial as a PDF:

Shell Scripting Tutorial
Buy the 72-page PDF (£4.99 / $9.99 / €6.99) of this tutorial
(Free Sample)

Get the Tutorial on Kindle:

For Kindle from Amazon USA:
For Kindle from Amazon UK:

And you can always Download my Free Shell Scripting Cheatsheet PDF

DefectiveByDesign.org