Wednesday, May 18, 2011

Binary Logic

Binary Logic is the subject covering logical operations which may be performed on binary (0 or 1) values. Sometimes it is called Boolean Logic because the field was pioneered by George Boole in the 1800's.

The Significance of 1 and 0 aside from being Numbers...

I would like to expand your understanding of what a binary digit represents. It is more than just a tiny number. We talked last lesson about how 1 represents the presence of electrical current and 0 represents its absence. In regular language these possibilities could be considered "on" and "off." A single bit (hint: bit is short for binary digit) taken by itself can actually be quite meaningful, when its values are considered in the following way:

0 = off = false = no
1 = on = true = yes

Think about how often we use yes or no to answer questions in our day to day life. In school you probably took many quizzes or tests featuring problems with True or False answers.

Binary Logic Operations

The simplest logical operation we can apply to a binary value is NOT.

Operation: NOT

NOT simply "flips" or "inverts" the bit:

NOT 1 = 0
NOT 0 = 1

Even though NOT operates upon a binary value, it is called a Unary operator because it only takes one input to achieve its output. The other operations in this lesson are binary operators because they take two inputs.

Operation: OR

So, now lets take a look at OR. In the world of binary logic, the outcome of X OR Y is true if either X or Y is true, or if both are true. In fact, the only way X OR Y can be false is if both of the inputs are false.

When we make a list of all the possible inputs and outputs, this is called a "Truth Table." The Truth Table for OR is:

0 OR 0 = 0
0 OR 1 = 1
1 OR 0 = 1
1 OR 1 = 1

Another way to look at it is like this:

X Y (X OR Y)
000
011
101
111

Operation: AND

The outcome of X and Y is true only if both inputs X and Y are true.

0 AND 0 = 0
0 AND 1 = 0
1 AND 0 = 0
1 AND 1 = 1

Operation: XOR

XOR means "Exclusive OR" The outcome of X xor Y is true if either of the inputs X or Y are true but NOT both of them.

0 XOR 0 = 0
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 0

You can apply a NOT to the result of the three binary operators we have learned so far to come up with three additional operations...

Operation: NOR

0 NOR 0 = 1
0 NOR 1 = 0
1 NOR 0 = 0
1 NOR 1 = 0

Operation: NAND

0 NAND 0 = 1
0 NAND 1 = 1
1 NAND 0 = 1
1 NAND 1 = 0

Operation: NXOR (also called XNOR)

0 NXOR 0 = 1
0 NXOR 1 = 0
1 NXOR 0 = 0
1 NXOR 1 = 1

Logical vs. Bitwise

In some programming languages you will discover that there are different operators for "logical" OR as opposed to "bitwise" OR (and likewise for all of the other operators.) I want to clear up the mystery behind this right now. Logical OR is what we've been talking about above. It takes the item on the left and right side of the OR and treats each as a true or false value, and gives you a true or false result. Bitwise OR is just a shortcut for doing a bunch of these at once. It takes the left side and the right side as two binary numbers, and performs a logical operation on each of the digits in sequence, for example:

Bitwise AND:

100101
&111011
========
100001

You will notice that each of the place values considered separately is simply a logical AND operation as explained previously.

Well, that's it...

The meaning of all the binary logic operators has been explained above. All other mathematical operations can be derived by applying these binary logic operations in different combinations.

Next Post: "Hello World" in PHP at the Command Line.

No comments:

Post a Comment