Sie sind auf Seite 1von 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

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

Write down 3, then

04 +09 13

3+3=6 3+4=7 3+5=8 ...

09+04 is 13, all done!

9 + 9 = 18

Addition in binary (Base 2)


01 +01
1 + 1 = 10

Base 10 single digit addition 0+0=0 0+1=1 1 + 1 = 10 What's 1 + 1 + 1?

Remember, 10 in binary = 2 in decimal

carry the 1

01 +01 0

Write down 0, then

01 +01 10

01+01 is 10, all done!

1+0+0=1

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.
8 4 2 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

Write down 0, then

0011 +0101 0
1 + 1 = 10

carry the 1

0011 +0101 00

Write down 0, then

0011 +0101 00
1 + 1 = 10

carry the 1

0011 +0101 000

Write down 0, then

0011 +0101 000


1+1+0=1

1000 in binary = 8 in decimal

0011 +0101 1000


8 4 2 1

1 0 0 0 +

0 0 1 1 0 1 0 1 1 0 0 0

3
+ 5

All done!

Check your work

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

Logical operations with binary numbers


Adding 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 other operations 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.
Input A

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

Meet the OR gate. How do you think the OR gate operates?

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 ?

If you guessed 1, you're right!

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

But instead of writing a "+" down, write "AND" 1


AND

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

Time to check your skills

0 AND 0 1 AND 0 0 AND 1 1 AND 1

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

Solve these. What is the nal output? The intermediate outputs?

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.

Das könnte Ihnen auch gefallen