binary packet 2
TRANSCRIPT
-
8/12/2019 Binary Packet 2
1/16
Binary Math!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.
Addition in decimal (Base 10)
04
+09
9+4 is 13
04
+09
3
1carry the 1
Write down 3, then
04
+09
13
1
09+04 is 13, all done!
Base 10
single digitaddition
0 + 0 = 00 + 1 = 10 + 2 = 20 + 3 = 30 + 4 = 40 + 5 = 50 + 6 = 60 + 7 = 7
0 + 8 = 80 + 9 = 9
1 + 1 = 21 + 2 = 31 + 3 = 41 + 4 = 51 + 5 = 61 + 6 = 71 + 7 = 81 + 8 = 9
1 + 9 = 10
2 + 2 = 42 + 3 = 52 + 4 = 62 + 5 = 72 + 6 = 82 + 7 = 92 + 8 = 102 + 9 = 11
3 + 3 = 63 + 4 = 73 + 5 = 8
...
9 + 9 = 18
-
8/12/2019 Binary Packet 2
2/16
Addition in binary (Base 2)
01+01
1 + 1 = 10
01+01
0
1carry the 1
Write down 0, then
01
+0110
1
01+01 is 10, all done!
Base 10single digit
addition
0 + 0 = 0
0 + 1 = 11 + 1 = 10
Remember,10 in binary = 2 in decimal
What's1 + 1 + 1?
1 + 0 + 0 = 1
-
8/12/2019 Binary Packet 2
3/16
0011
+0101
01+01 is 10, all done!
Binary addition with larger numbers
It's great to be able to add numbers like 1 and 1, but how about
adding larger numbers like 3 + 5?
First, convert the numbers to binary.
3 in decimal converts to 11 in binary.
5 in decimal converts to 101 in binary.
0 1 1
124
= 3
1 0 1
124
= 5
1 + 1 = 10
1carry the 1
Write down 0, then
0011
+01010
0
8
0
8
-
8/12/2019 Binary Packet 2
4/16
1
0011
+0101
0
1 + 1 = 10
1carry the 1
Write down 0, then
0011
+010100
1
0011+0101
00
1 + 1 = 10
-
8/12/2019 Binary Packet 2
5/16
1carry the 1
Write down 0, then
0011
+0101
000
1
0011
+0101000
1 + 1 + 0 = 1
1
0011
+0101
10001000 in binary = 8 in decimal
0 0 0
124
1
8
0 1 1
124
0
8
1 0 10
0 0 01
+
3
5
8
All done!
+
-
8/12/2019 Binary Packet 2
6/16
0 1 11
1 0 11
0
0+
0 1 011
Check your work
11
13+
24 0 1 0
124
1
8
1
1624 in decimal
is equal to
11010 in binary,
so our math is right!
0 1 10
0 0 11
0
0+
0 1 11
1 1 10
0
0+
0 0 11
1 0 11
0
0+
1 1 11
1 0 11
0
0+
1 1 11
0 0 01
0
0+
0 0 01
1 1 10
0
0+
0 1 10
0 0 11
0
0+
0 0 11
0 0 11
0
0+
-
8/12/2019 Binary Packet 2
7/16
1 1 11
0 0 01
0
0+
0 1 11
1 1 10
0
0+
1 1 10
1 0 11
0
0+
1 0 01
1 0 01
0
0+
0 0 11
1 0 11
0
0+
0 0 10
1 1 11
0
0+
0 0 10
0 1 11
0
0+
1 1 00
1 1 00
0
0+
0 1 11
1 0 11
0
0+
0 0 11
1 1 11
0
0+
0 1 10
1 0 10
0
0+
1 1 11
1 0 10
0
0+
0 0 01
1 0 01
0
0+
0 1 11
1 0 10
0
0+
1 0 10
0 1 01
1
0+
-
8/12/2019 Binary Packet 2
8/16
Logical operations with binary numbersAdding is a really helpful operation to have, but are there other ways
to compare binary numbers?
Of course there is! Why else would we have an entire sheet on otheroperations if they didn't exist?
So besides adding binary numbers, you can perform so called logical
operations on those numbers.
We will be learning about these operations:
- AND
- OR
- NOT- XOR
A logical operation compares binary numbers and gives a result
based on whatever operation it's implementing.
Let's start with AND. It's helpful to think of the logical operation as a
"gate". This AND gate has two inputstwo places where you can
input binary numbers and a single output. Logic gates ALWAYS have
one single output though they may have many inputs.
AND
Input A
Input B
Output
-
8/12/2019 Binary Packet 2
9/16
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.
AND
1
11
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.
AND1
00 AND
0
10 AND
0
00
Meet the OR gate. How do you think the OR gate operates?
OR
Input A
Input B
Output
OR
What do you think about this? What's the output for an OR gate that
has a 1 and a 0 as inputs?
1
0
?
-
8/12/2019 Binary Packet 2
10/16
OR
1
0
1
If you guessed 1, you're right!
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.
OR0
1
1 OR1
1
1 OR0
0
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.
The NOT gate is easy to understand it just flips the binary number.
If you input a 1, a NOT gate outputs a 0. If you give it a 0, it outputs
1.
Input OutputNOT
0 1NOT 1 0NOT
-
8/12/2019 Binary Packet 2
11/16
And finally, 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.
XOR
1
0
1
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.
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.
XOR0
11 XOR1
10 XOR0
00
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?
-
8/12/2019 Binary Packet 2
12/16
0 1 10
1 1 01
1
0
It's easy to get started just write the numbers down like you are
going to add them.
But instead of writing a "+" down, write "AND"
0 1 10
1 1 01
1
0AND
Now just compare them, using AND as a logical operator, byte by
byte, writing down the answer to each paring underneath.
0 1 10
1 1 01
1
0AND
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, 0 1 10
1 1 01
1
0
0 1 000
AND
Which you can write as "10011 AND 01110 = 00010"
-
8/12/2019 Binary Packet 2
13/16
So what is 10011 OR 01110?
How about 10011 XOR 01110?
How about NOT 10011?
Or NOT 011110?
Try these.
0 1 10
0 0 10
1
1AND
1 1 00
1 0 01
1
1AND
1 0 10
1 1 10
1
0AND
1 0 10
1 1 01
1
0OR
0 0 10
0 1 00
1
0OR
0 1 11
1 1 11
1
1OR
0 1 10
0 1 11
1
0XOR
1 0 10
0 0 11
1
0XOR
1 1 11
1 0 01
1
1XOR
0 1 011NOT
0 1 100NOT
1 1 011NOT
-
8/12/2019 Binary Packet 2
14/16
XOR
1
0
1 NOT
OR0
0AND
0
0
AND1
0
AND
0
1
AND1
1
OR1
0
OR1
1
0 NOT
XOR
0
1
XOR
1
1
XOR
0
0
Time to check your skills
-
8/12/2019 Binary Packet 2
15/16
OR0
1
XOR
1
0
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 happensbecause of chained logic gates performing arithmetic and logical operations. Comparing andadding numbersthat's all it is.
AND NOT
1
1
1 0
OR0
1
XOR
1
1
ANDNOT
0
1
0 1
Change the inputs and you can get a different output.
-
8/12/2019 Binary Packet 2
16/16
ORXOR
1
1
AND
NOT
Solve these. What is the final output? The intermediate outputs?
1
0
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 NOTgates.
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 ofthe circuit.
AND
OR
XOR
NOT
1
1
1
1