 # Computational Logic - A Beginners Guide to Logic Gates - Part 1

Every article needs to start somewhere. This would be the logical place to do it. Logic in electronics and computer science is all about answering questions posed by 1’s and 0’s. So before we get stuck in lets have a quick refresher on how binary logic works.

## Bit’s and Pieces

Binary is, at it’s heart, one of two options. 1 or 0, ‘Yes’ or ‘No’, True or False. Using combinations of these options we can encode a wealth of information.

## Learning to Count - Easy as 01, 10, 11

Now we could always encode numbers as a series of 1’s. 1 would be 1, 4 would be 1111, and 100 would be more than I’m in the mood for counting right now. But you can see how quickly a simple calculation would eat through space. This is why binary numbers are encoded as powers of 2.

### Binary - Decimal Table

Digit282726252423222120
0000000000
1000000001
2000000010
3000000011
4000000100
5000000101
6000000110
7000000111
8000001000
9000001001
10000001010
11000001011
12000001100
13000001101
14000001110
15000001111
16000010000
...
255011111111
256100000000

As the table above shows, the bit furthest to the right is the least significant bit. This will come into play later. This is all well and good but it’s quite hard to decipher for a human, so we often group these bits into groups of 8 (bytes) and sub-divide them still into groups of 4 (nibbles). Therefore, 0100 1001 translates to 73! And 49, and Capital i… and just a bit less than 29% fully red. Ok what’s going on

## Let me ASCII you something

As we mentioned above, the binary input means only what we tell it it means. So when we encode or decode binary it’s ‘meaning’ is all dependant on its encoding.

One of the most famous examples of binary encoding is the American Standard Code for Information Interchange, more commonly called, ASCII. The idea of ASCII was to encode the most commonly used characters like Letters, Numbers, Symbols and a handful of control characters. We only had 128 addresses in the original encodings of ASCII so it’s nowhere near as fleshed out as unicode is today, but it still got the job done back in the day.

Theres a really smart thing I like about how ASCII characters were encoded. Numbers 0 – 9, letters A-Z and their lowercase counterparts, all have rather logical binary codes. The 4 least significant bits of the digits 0 – 9 are actually the same as the value of the number in binary. ie. 5 = 0101. The letters are similar in that both upper and lower case ‘A’ begins on 0001. However the three most significant bits denote what the number or letter is. ie. 5= 011 0101, uppercase ‘A’ is 100 0001 and lowercase ‘a’ is 110 0001. People proficient in counting in binary can quickly convert a letter or a number in their heads, Now theres a party trick.

## And? Now what

Ok, so back to logic. Sometimes we need to find out what the result of some binary inputs would be after performing a certain function. The simplest operation we can perform is called the ‘NOT’. This is simply an inversion of the signal going in. If a is High we pull it low, if it’s low we pull it high. Pseudo-code to follow.

` // A C-like representation of a NOT gate bool a = true; // boolean operator set to true bool q; // boolean variable q setup to give an output if(a == true) // Check to see if a is true, if it is do the following condition q = false; // q set to false else // Check to see if a is true, if it isn't, then do this part q = true; // q set to true ` ## AND Gate  ## NAND Gate  ## XOR Gate  ## Logic Gates - Practically

So we’ve looked at binary, We’ve looked at the different types of gates available. Lets look now and finding a practical use for logic gates.

## Pick a number, Any number

One of the simplest decoders one can build is using NOT Gates. If we take a signal, and the inverse of the signal we can check if parallel lines create a state. This, in conjunction with an AND gate can give us an output if the condition is met.

Let’s take a simple 4 bit counter, and try and find the number 13. If the number is 13 this counter will trigger another circuit to ring a buzzer or release the hounds, whatever it is you need it to do.

So the first thing we look for is the signal coming from the output of our Binary Lines, 13 in binary would be made up of 8 + 4 + 1, therefore, we’re looking for 1101 on our output.

Now we know an AND gate will only go high if ALL the inputs are active, So lets invert our 1101. Now we’ve got 0010. Wait. That’s not all the inputs either! Sure, it would be simpler just to invert the 0, but if we invert all the signals we can take outputs for any number of different states.

So in this case we take the first two outputs as they are. The third bit we take the inverted version, and for the least significant bit we take the normal output again. Now we have, (when our counter hits 13) 4 lines that are all in the active state. All thanks to our NOT logic gate and our counter.

Now, if we take a Quad Input AND Logic Gate – yes, it is a thing – and feed our ’13’ signal in, we get a positive output to whatever we need to send it too. Simple! It is even expandable enough that when we need to tap more numbers for different instructions we’ve already set up the infrastructure.

## To Sum Up

One of the most popular circuits for beginners in the world of engineering and computational logic is building a “half adder” and a “full adder”.

Addition and subtraction are the two most fundamental mathematic operations one can perform. So it’s only natural that in a world of computers, smart phones and increasingly popular “Calculators”, we have discovered an efficient way to add binary numbers together using logic gates.

Check our next instalment where we will discuss how to build these circuits in a little more detail.