Download - Binary Numbers

Transcript
Page 1: Binary Numbers

2-1

Recall . . .Recall . . .

• The definition of a computer• Analog/Digital• Electric/Mechanical• General Purpose/Special Purpose• The General Purpose Electronic Computer• General Purpose Binary• Binary?

Page 2: Binary Numbers

2-2

Page 3: Binary Numbers

2-3

Binary CircuitryBinary Circuitry

• Binary circuitry: – cheap

– reliable

– able to be extended to very complicated logic

• built on only two states

» ON (1)

» OFF (0)

Page 4: Binary Numbers

2-4

Computers work in BinaryComputers work in Binary

• Computers are not only powered by electricity they “compute” with electricity– they shift voltage pulses around internally

– circuits allow for electricity to flow or to be blocked depending on the type of circuit

Closedcircuit

Opencircuit

ON or 1 OFF or 0

Page 5: Binary Numbers

2-5

Representation of DataRepresentation of Data

• So, our binary computer can represent:– 0s and 1s. . .

– We need to represent considerably more than that:• Numbers

• Characters

• Visual Data

• Audio Data

• Instructions

… and we need to do it with only 0’s and 1’s

Page 6: Binary Numbers

2-6

Representation of NumbersRepresentation of Numbers

• Representing numbers is considerably more than something that looks like the symbol “1” or “2” or “430”

– We’re trying to represent numbers; which have conceptual meaning

9 = 3+3+3 = 4+5 = 10-1 = 3*3 = 3^3 = 9

Page 7: Binary Numbers

2-7

Representation of NumbersRepresentation of Numbers

• Decimal numeration system: (aka base 10)

– Uses 10 symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.– The place values of each position are increasing powers of ten.

• A number such as 1428 literally means:– Eight Ones– Two Tens– Four Hundreds– One (A single) Thousand

= (1 x 1000) + (4 x 100) + (2 x 10) + (8 x 1)

1010101010 01234

110100100010000

Page 8: Binary Numbers

2-8

CombinationsCombinations

• Imagine we have three light-bulbs in a row, and each bulb can be on (1) or off (0).

• How many unique combinations of lights can we have?– (Hint, start with all lights off)

Page 9: Binary Numbers

2-9

CombinationsCombinations

• The number of unique combinations we can have of one light with two states per lights is two:

• The number of unique combinations we can have of two lights with two states per light is four:

• The number of unique combinations we can have of three lights with two states per lights is eight: 000

001

010

011

100

101

110

111

0

1

00

01

10

11

Page 10: Binary Numbers

2-10

Representation of NumbersRepresentation of Numbers

• Our three-light system– has eight possible combinations of

on and off.

• With eight unique combinations, we could represent the numbers

0, 1, 2, 3, 4, 5, 6, 7

0 = 000 4 = 100

1 = 001 5 = 101

2 = 010 6 = 110

3 = 011 7 = 111

Page 11: Binary Numbers

2-11

Representation of NumbersRepresentation of Numbers

• Binary numeration system (aka base 2):– Will use 2 symbols: 0, and 1.

(Each is called a bit for binary digit)– The place values of each position are powers of two.

– A binary number such as 10110two will be expanded as:

• Zero Ones• One Two• One Four• Zero Eights• One Sixteen

= (1 x 16) + (0 x 8) + (1 x 4) + (1 x 2) + (0 x 1) = 22 in decimal

22222 01234

124816

0 1 1 01

Page 12: Binary Numbers

2-12

Binary-to-Decimal ConversionBinary-to-Decimal Conversion

• Convert the following binary number (base two) in decimal (base ten)

1 0 0 0 0 0 1 1

Page 13: Binary Numbers

2-13

Binary ConversionBinary Conversion

1 0 0 0 0 0 1 1

• Step 1: Make a table with the same number of columns as places in the binary string and copy the string into the table

1 0 0 0 0 0 1 1

Page 14: Binary Numbers

2-14

Binary ConversionBinary Conversion

• Step 2: Write out the powers of two corresponding to each position in the binary number:

27 26 25 24 23 22 21 20

1 0 0 0 0 0 1 1

Page 15: Binary Numbers

2-15

Binary ConversionBinary Conversion

• Step 3: Write out the powers of two corresponding to each position in the binary number in decimal:

27 26 25 24 23 22 21 20

128 64 32 16 8 4 2 1

1 0 0 0 0 0 1 1

Page 16: Binary Numbers

2-16

Binary ConversionBinary Conversion

• Step 4: multiply the second and third rows and put the result in the fourth row:

27 26 25 24 23 22 21 20

128 64 32 16 8 4 2 1

1 0 0 0 0 0 1 1

128 0 0 0 0 0 2 1

Page 17: Binary Numbers

2-17

Binary ConversionBinary Conversion

• Step 5: (final step) – Add up all the numbers in the fourth row

27 26 25 24 23 22 21 20

128 64 32 16 8 4 2 1

1 0 0 0 0 0 1 1

128 0 0 0 0 0 2 1

128+0+0+0+0+0+2+1 = 131

Page 18: Binary Numbers

2-18

Page 19: Binary Numbers

2-19

Decimal to BinaryDecimal to Binary

2(number of places) = number of unique combinations we can achieve with some number of places in binary; but we have to use one of the mappings for zero so. . .

2(number of places) - 1 = largest binary number we can store with that many places

1 21 – 1 = 1 with 1 place we can store numbers from 0 to 1

5 25 – 1 = 31 with 5 places we can store numbers from 0 to 31

7 27 – 1 = 127 with 7 places we can store numbers from 0 to 127

8 28 – 1 = 255 with 8 places we can store numbers between 0 and 255

Page 20: Binary Numbers

2-20

Binary ConversionBinary Conversion

Step 2: Write out a binary conversion table with n many places, and fill in the values of the first two rows:

27 26 25 24 23 22 21 20

128 64 32 16 8 4 2 1

Page 21: Binary Numbers

2-21

Binary ConversionBinary Conversion• Step 3: Subtract out the decimal powers of two from left to

right. – If you can subtract that amount and the result is non-negative, write a 1 in

the binary string and continue with the result– If the subtraction results in a negative number, write a 0 and continue

with the last positive number

27 26 25 24 23 22 21 20

128 64 32 16 8 4 2 1

245-128 = 117

117 - 64 = 53

53 - 32 = 21

21 - 16 =

5

5 - 8 = -3 error

5 - 4 = 1

1 - 2 = -1 error

1 - 1 = 0

1 1 1 1 0 1 0 1

245

Page 22: Binary Numbers

2-22

Addition/SubtractionAddition/Subtraction

• Addition in any number system. . . – We add in the places from right to left– If the sum of the two numbers exceeds the symbols we have at our

disposal we “carry” some amount. . .

4 + 8 = “twelve” ; in base ten, we have no single numeric symbol that equals twelve

• We don’t need to express “twelve” in a single symbol, because we can do so by breaking the number down and incrementing the base

• Instead we write 12 which is: One Ten and Two Ones.

• Logically, we subtract 10 (the base amount) from our sum, and write the result in that place, and we carry a one into the next place, which represents One additional ten (our base number)

Page 23: Binary Numbers

2-23

Binary AdditionBinary Addition

• Addition of binary numbers:0 + 0 = 00 + 1 = 11 + 0 = 11 + 1 = 2ten How do we write this?

So we’ll carry the symbol 1 into the next place (which represents two in decimal), and we’ll write down the sum minus our base amount (2)

1 + 1 = 10two

Example: adding two binary numbers

Page 24: Binary Numbers

2-24

Binary SubtractionBinary Subtraction

• Subtraction of binary numbers:

0 - 0 = 00 - 1 = -1 problem! We need to borrow …

1 - 0 = 11 - 1 = 0

Remember when borrowing in base 10:

– We decrease the symbol to the left by one

– Remember when we borrow 1 from the symbol to our left, it has a value that’s “ten more” (or the base amount)

Page 25: Binary Numbers

2-25

Binary SubtractionBinary Subtraction

• Let’s subtract the following numbers:

1 0 0 0 0 0 1 1 - 0 1 0 0 0 1 0 0 ------------------

Page 26: Binary Numbers

2-26

Binary SubtractionBinary Subtraction

• We need to borrow for the third term, but the eighth is the closest term with something to borrow from!

1 0 0 0 0 0 1 1 - 0 1 0 0 0 1 0 0 ------------------ 1 1

Page 27: Binary Numbers

2-27

Binary SubtractionBinary Subtraction

• So, we borrow 2ten from the first place we can

0 2 1 0 0 0 0 0 1 1 - 0 1 0 0 0 1 0 0 ------------------

Page 28: Binary Numbers

2-28

Binary SubtractionBinary Subtraction

• Keep borrowing two

1 0 2 2 1 0 0 0 0 0 1 1 - 0 1 0 0 0 1 0 0 ------------------ 1 1

Page 29: Binary Numbers

2-29

Binary SubtractionBinary Subtraction

• Keep borrowing 2 . . .

1 1 0 2 2 2 1 0 0 0 0 0 1 1 - 0 1 0 0 0 1 0 0 ------------------ 1 1

Page 30: Binary Numbers

2-30

Binary SubtractionBinary Subtraction

• Keep borrowing 2 . . .

1 1 1 0 2 2 2 2 1 0 0 0 0 0 1 1 - 0 1 0 0 0 1 0 0 ------------------ 1 1

Page 31: Binary Numbers

2-31

Binary SubtractionBinary Subtraction

1 1 1 1 0 2 2 2 2 2 now, we’re able to subtract here

1 0 0 0 0 0 1 1 - 0 1 0 0 0 1 0 0 ------------------ 1 1 1

Page 32: Binary Numbers

2-32

Binary SubtractionBinary Subtraction

• The rest of the subtractions are easy . . .

1 1 1 1 0 2 2 2 2 2 1 0 0 0 0 0 1 1 - 0 1 0 0 0 1 0 0 ------------------ 0 0 1 1 1 1 1 1

So our answer is -> 00111111Let’s convert to decimal to check our work

Page 33: Binary Numbers

2-33

Binary SubtractionBinary Subtraction

• converting binary to decimal we get. . .

1 0 0 0 0 0 1 1 -> 128+2+1 = 131 - 0 1 0 0 0 1 0 0 -> 64+4 = 68------------------ 0 0 1 1 1 1 1 1 -> 32+16+8+4+2+1 = 67

131 – 68 = 67

Page 34: Binary Numbers

2-34

Binary NumbersBinary Numbers

• Binary numbers actually have some other neat properties as well . . .

– QUESTION: Can you multiply a binary number by two (decimal) and give the result in binary quickly?

• Multiply the number 00110 by two (and give the answer in binary)

– Why does this work?

22222 01234

124816

0 1 1 00

25

32

0


Top Related