Beruflich Dokumente
Kultur Dokumente
We will be looking at two kinds of binary number operations arithmetic (addition) operations and logical operations. We're going to learn how to add numbers and compare numbers. First, we talk about adding numbers.
Base 10 single digit addition 0+0=0 0+1=1 0+2=2 0+3=3 0+4=4 0+5=5 0+6=6 0+7=7 0+8=8 0+9=9 1+1=2 1+2=3 1+3=4 1+4=5 1+5=6 1+6=7 1+7=8 1+8=9 1 + 9 = 10 2+2=4 2+3=5 2+4=6 2+5=7 2+6=8 2+7=9 2 + 8 = 10 2 + 9 = 11
carry the 1
04 +09 3
04 +09 13
9 + 9 = 18
carry the 1
01 +01 0
01 +01 10
1+0+0=1
0
8
0
4
1
2
1
1
=3
=5
0011 +0101
01+01 is 10, all done! 1 + 1 = 10
carry the 1
0011 +0101 0
0011 +0101 0
1 + 1 = 10
carry the 1
0011 +0101 00
0011 +0101 00
1 + 1 = 10
carry the 1
1 0 0 0 +
0 0 1 1 0 1 0 1 1 0 0 0
3
+ 5
All done!
1 1
0 1
1 0
1 1
11
+ 13 16
24 in decimal is equal to 11010 in binary, so our math is right!
1 1 0 1 0
24
1 1 0 1 0
0 1
0 0
1 0
1 1
1 1
1 0
1 0
1 0
1 0
0 1
1 1
1 1
1 0
0 1
0 1
0 1
1 1
0 1
0 0
1 1
0 1
0 0
1 0
1 1
1 1
1 1
1 0
1 1
1 1
0 0
0 0
1 1
1 1
1 0
1 0
1 0
1 1
0 1
0 0
1 1
1 1
0 1
1 0
1 1
1 0
0 1
1 1
1 1
0 1
0 1
0 1
1 1
1 1
0 1
0 1
1 1
0 1
1 1
1 0
1 1
0 1
0 0
0 1
1 1
0 0
0 1
1 0
1 1
1 1
1 1
0 0
0 0
0 0
1 1
1 1
0 0
1 0
1 1
1 0
1 1
1 1
0 1
0 0
0 0
1 0
0 1
1 0
1 1
0 1
1 0
0 1
1 0
AND
Input B
Output
Logic gates make decisions. The AND logic gate will output a 1 if both of its inputs are 1. It outputs 1 if both Input A AND Input B are 1.
1 AND 1
If you pass a 1 and a 0 as inputs to an AND gate, it will output 0. It needs every input to be 1 before it will output a 1.
1 AND 0 0
0 AND 1 0
0 AND 0 0
Input A
OR
Input B
Output
What do you think about this? What's the output for an OR gate that has a 1 and a 0 as inputs?
1 OR 0 ?
1 OR 0
An OR gate outputs a 1 if any of its inputs are 1. An OR gate outputs a 1 if Input A OR Input B are 1.
0 OR 1 1
1 OR 1 1
0 OR 0
This is the NOT gate. It's unique in that it can only receive a single input. Like other logic gates, it has just one single output.
Input
NOT
Output
The NOT gate is easy to understand it just ips the binary number. If you input a 1, a NOT gate outputs a 0. If you give it a 0, it outputs 1.
NOT
NOT
And nally, we have XOR. XOR stands for eXclusive OR. XOR is a little more complicated than just a simple OR. XOR only outputs a 1 if and only if one of its inputs is 1. If both inputs are 1, it outputs 0.
1 XOR 0
An OR gate outputs a 1 if any of its inputs are 1. An OR gate outputs a 1 if Input A OR Input B are 1.
0
XOR
1 1 1
XOR
0 0 0
XOR
We will be using these logical operations/gates AND, OR, NOT, and XOR because they can be used to create any kind of digital circuitry magic that we can imagine. There are other logic gates like NAND, NOR, and XNOR that you can explore on your own. One of the cool things about binary operations is that you can perform them without using gates and you can apply them to multibyte numbers. For instance, say we wanted to run the logical operation AND on 10011 and 01110. How could we do that?
It's easy to get started just write the numbers down like you are going to add them. 1 0 0 1 0 1 1 1 1 0
0 1
0 1
1 1
1 0
Now just compare them, using AND as a logical operator, byte by byte, writing down the answer to each paring underneath.
1
AND
0 1
0 1
1 1
1 0
1 AND 0 outputs 0 1 AND 1 outputs 1 0 AND 1 outputs 0 0 AND 1 outputs 0 1 AND 0 outputs 0
So,
1
AND
0 1
0 1
1 1
1 0
0 0 0 1 0
Which you can write as "10011 AND 01110 = 00010"
So what is 10011 OR 01110? How about 10011 XOR 01110? How about NOT 10011? Or NOT 011110? Try these.
1
AND
0 0
0 0
1 0
1 1
AND
1 1
0 1
1 1
1 0
0 0
AND
1 0
0 0
1 1
0 1
1 1
1
OR
0 1
1 1
0 1
1 0
OR
1 0
0 0
0 0
0 1
1 0
OR
1 1
1 1
0 1
1 1
1 1
1
XOR
0 1
0 0
1 1
1 1
XOR
1 0
0 1
1 0
0 0
1 1
XOR
1 1
1 1
1 1
1 0
1 0
NOT
NOT
NOT
0 OR 0 1 OR 0
1 OR 1
NOT
NOT
1
XOR
1
XOR
0 0
XOR
1 0
XOR
And here is where the real magic starts. Logic gates can be chained together. Outputs can feed into inputs. By chaining logic gates, you can do anything a computer is capable of. Everything you do with a digital device happens because of chained logic gates performing arithmetic and logical operations. Comparing and adding numbersthat's all it is.
1
XOR
1 AND 1
NOT
0 0 0 OR 1
Change the inputs and you can get a different output.
1
XOR
0 AND 0
NOT
1 1 0 OR 1 1
1 AND 0 1
XOR
OR
NOT
1 1 1
NOT
OR
AND
XOR
Now draw your own. A circuit with four inputs, two OR gates, one AND gate, and one NOT gate. A circuit with six inputs, three XOR gates, two AND gates, one OR gate, and three NOT gates. A circuit with eight inputs, one XOR gate, six NOT gates, one AND gate, and two OR gates. Populate the starting inputs with values and see if your neighbor can work through the logic of the circuit.