Binary Logic

Today I want to talk in more detail about binary logic. Binary logic is the most fundamental thing on which a CPU is based. As soon as you understand binary logic, you will have everything you need to implement a fully functional CPU. Electronic circuits use so-called Logic Gates to implement the binary logic.

Binary logic is sometimes also referred as Boolean Logic. Boolean logic was discovered and invented by George Boole – hence its name. Today I want to show you the 4 simplest binary operations around which we will build our CPU:

  • AND
  • OR
  • XOR
  • NOT

With these 4 different operations, in time you will be able to build a fully functional CPU that executes your custom written assembly code. Awesome and unbelievable, isn’t it? Every binary operation accepts input values, and gives back an output value. Both input and output values are just simple bit values: 0 or 1. Therefore they can be represented quite easily with an electronic circuit.

When current flows through an electronic circuit, a binary value of 1 is represented. When no current flows, a binary value of 0 is represented. That’s how modern CPUs work nowadays – at a high level.

Every time we talk about binary logic, we also have to talk about so-called Truth Tables. A truth table for a boolean operation defines the possible input values and the expected output values. Even the basic binary operations have their associated truth tables. Later on we will also cover more complex truth tables, for example when we implement a fully functional ALU (Arithmetical Logic Unit) out of the basic boolean operations that I am covering in today’s blog post.

Now let’s have a more detailed look at these 4 basic operations.

AND

The AND operation returns a bit value of 1 when both input bit values are set to 1. Otherwise a bit value of 0 is returned by this operation. Therefore the AND operation has the following truth table:

Input A Input B Output
0 0 0
0 1 0
1 0 0
1 1 1

The AND operation is used very often to test if a specific bit in a bit pattern is set. Imagine you want to know if the 1st right bit in the bit pattern 1101 is set. In that case you can perform the following AND operation:

1101 AND 0001

The AND operation will return the bit pattern 0001 and so you can see that the 1st right bit was set.

OR

The OR operation returns a bit value of 1 when one or both input bit values are set to 1. Otherwise a bit value of 0 is returned by this operation. The truth table for the OR operation looks like the following:

Input A Input B Output
0 0 0
0 1 1
1 0 1
1 1 1

The OR operation is used very often to set a specific bit in a bit pattern. Imagine you want to set 1st right bit in the bit pattern 1100 to 1. In that case you can perform the following OR operation:

1100 OR 0001

The OR operation will return the bit pattern 1101 and you can see that the 1st right bit is now set.

XOR

The XOR operation returns a bit value of 1 when just one of both bits is set. If no bit is set or both bits are set, it returns a bit value of 0. The truth table of the XOR operation looks like the following:

Input A Input B Output
0 0 0
0 1 1
1 0 1
1 1 0

The XOR operation is used very often to clear the content of a specific register. Imagine the following assembly instruction:

XOR AL, AL

When you execute this instruction on a CPU, the content of the register AL will be cleared, because the output of the XOR operation is a bit 0 when you provide the same input bit both times.

NOT

The NOT operation is the simplest because it just negates the input: a bit 0 will become a bit 1, a bit 1 becomes a bit 0. Therefore the truth table is:

Input Output
0 1
1 0

The NOT operation is also an operation which just accepts one input. You can use the NOT operation to perform a logical negation, the so-called Ones’ Complement:

NOT 1101

The NOT operation here will return the bit pattern 0010, which is just the logical negation of 1101.

Summary

With the 4 basic operations AND, OR, XOR, and NOT you have now seen all the operations that are needed to build a CPU! Of course, the various operations have to be combined together into more sophisticated ones to perform any useful work. But you will see this over the next blog posts with more concrete examples.

Thanks for your time,

-Klaus

Comments are closed.