number systems and binary math. why do computers use switches? why did engineers select a...

109
Number Systems and Binary Math

Upload: dennis-mcdowell

Post on 14-Jan-2016

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Number Systems and Binary Math

Page 2: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Why do computers use switches?

Why did engineers select a binary-state (two state) switch as the fundamental computer component building block.

It was simple and reliable and it supported built-in error checking.

Page 3: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Adding Switches Doubles Capacity

1 switch = 2 numbers 2 switches = 4 numbers 3 switches = 8 numbers 4 switches = 16 numbers

Page 4: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Units of Computer Measurement

The smallest unit of computer measurement is a binary digit (or more commonly, the “bit”).

The bit represents a single binary digit, either a 1 or a 0.

Page 5: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Larger Units of Measurement

Bit = A single binary digit Nibble = 4 Bits Byte = 2 Nibbles = 8 Bits Word = 2 Bytes = 16 Bits Double Word = 2 Words = 32 Bits Kilobyte = 1024 Bytes Megabyte = 1024 Kilobytes Gigabyte = 1024 Megabytes

Page 6: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Starting with Numbers

Each switch can either have sufficient voltage (“ON”) or have insufficient voltage (“OFF”).

Another way of stating voltage: 1 or 0 This is a common approach in science – to use abstract,

notational systems to describe things.

1

0

Page 7: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Numbering Systems

To figure the answer to this question, let’s step back and look at numbering systems in general.

Originally, human being counted with tally math systems, with a mark for each sheep they were counting If you had 10 sheep, you made 10 marks

Page 8: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Numbering Systems

Later, there were advancements in tally math, using special symbols for special numbers. Instead of 10 marks for 10 sheep, let’s make a

special mark for 10 sheep …

But this notational system was cumbersome… you had to learn a lot of symbols, and arithmetic was difficult

==XX

Page 9: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Positional Numbering Systems

Wonderful advancement over tally math. Let’s keep a few, core numbers (digits). We can use these digits to represent REALLY

big numbers if we say that the value of a number is now made up of 2 parts: A count value A place value

Page 10: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Place Values

Consider the number 37… The 7 stands for 7 (it’s count value) ones (it’s

positional value). Together, 7 times 1 is 7

3 73 7

Page 11: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Place Values

Consider the number 37… The 3 stands for 3 (it’s count value) tens (it’s

positional value). Together, 3 times 10 is thirty

3 73 7

Page 12: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Place Values

Consider the number 37… Total value: 7 (7 * 1, from the ones

position) and thirty (3 * 10, from the 10 position) is 37!

3 73 7

Page 13: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Place Values

3 73 7“O

nes

”“Ten

s”

7 * 13 * 10

Page 14: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Base-10 (Decimal)

A base refers to the number of digits (counting numbers) available in a numbering scheme.

The highest single digit in base is one less than the base number.

In Base-10, the digits are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Page 15: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Base-10 (Decimal)

What happens after 9? The counter in the right column resets itself to

“0” and 1 is added to the value of the next left-most column:

00 9 9 1 01 0

Page 16: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Base-10 (Decimal)

Interpreting a multiple digit number simply means to expand its notation, and write it out by each positional value:

7 4 27 4 2“Ones”2 * 1

“Tens”4 * 10

“Hundreds”

7 * 100

Page 17: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Base-10 (Decimal)

What about using some math to describe the number:

7 4 27 4 210

02 * 1004 * 1017 * 102

101

102

Page 18: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Using Expanded Notation

We can use our expanded notation to figure the value of a number:

7*107*1022 = 7*100 = 700 = 7*100 = 700

4*104*1011 = 4*10 = 40 = 4*10 = 40

2*102*1000 = 2*1 = 2 = 2*1 = 2

= 742= 7421010

Page 19: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

A Note on Superscripts

We’ve seen superscripts in expressions before today. In math, they are used to indicate an exponent in an expression:

xxyy

Page 20: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

A Note on Subscripts

Let’s introduce one more notation – the subscript. It is used to indicate the base of a number (the number of digits available in a numbering system):

xxyy

Page 21: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

A Note on Subscripts

Remember our previous example?

It had a subscript of “10”, meaning that the number we use the Base-10 (Decimal) numbering system to decode this number.

7427421010

Page 22: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Thinking in Binary (Base-2)

Instead of having 10 digits, the binary (Base-2) numbering systems has 2 base digits (0 and 1). Counting in Base-2: 0, 1, 10, 11, 100, 101, 110,

111, 1000, 1001, 1010 … In binary, after the right-most placeholder reaches one, it

resets to 0 and advances the next left-most placeholder. Our base number in binary is 2, therefore we can use

exponents of 2 for placeholder notation

Page 23: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Binary Placeholders

1 0 1 11 0 1 1“T

wo

s”

“On

es”

“Fo

urs

“Eig

hts

Let’s deconstruct 10112:

Page 24: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Binary Placeholders

1 0 1 11 0 1 120

Now, let’s use exponents:

212223

Page 25: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting Base-2 to Base-10

Let’s convert 10112.

STEP 1: Find all switches that are turned “OFF”. Cross them out and bring down the zero.

1 0 1 11 0 1 120212223

00

Page 26: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting Base-2 to Base-10

STEP 2: For each placeholder turned “ON”, calculate its exponential expression.

1 0 1 11 0 1 120212223

00 112288

Page 27: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting Base-2 to Base-10

STEP 3: Add the results of the exponents. The sum is the Base-10 equivalent.

1 0 1 11 0 1 120212223

00 112288++ ++ ++ ==11111010

Page 28: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting from Base-10 to Base-2

Consider 2310. How can we translate this to its Base-2 equivalent?

We’ll use successive division. Essentially, that means we’ll divide by 2 over and over and keep track of our remainders.

Page 29: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting Base-10 to Base-2

STEP ONE: Draw a table with three columns. Label the last two columns quotient and remainder, respectively.

Q R

Page 30: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting Base-10 to Base-2

STEP TWO: In the first available left-most column, put the expression 23/2. Calculate the division and put the quotient and remainder in their respective columns.

Q R

23/2 11 1

Page 31: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting Base-10 to Base-2

STEP THREE: Take the quotient of the previous expression and divide it by two. Repeat until have 0 for a quotient.

Q R

23/2 11 1

11/2 5 1

5/2 2 1

2/2 1 0

1/2 0 1

Page 32: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting Base-10 to Base-2

STEP FOUR: Read the REMAINDER column from the bottom to the top. The 1s and 0s in the remainder column represent the binary number.

Q R

23/2 11 1

11/2 5 1

5/2 2 1

2/2 1 0

1/2 0 1

Page 33: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting Base-10 to Base-2

2310 = 101112

Q R

23/2 11 1

11/2 5 1

5/2 2 1

2/2 1 0

1/2 0 1

Page 34: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Checking the Results

So.. 2310 = 101112!

Let’s check our answer by reversing the process

101112 = (1 * 24) + (0 * 23 ) + ( 1 * 22 ) + (1 * 21) + (1 * 20) =

16 + 0 + 4 + 2 + 1 = 2310

Page 35: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Groups of Switches

We have already seen that a single switch can’t encode much information. However, if we group switches together, we can expand our encoding ability.

Computers often encode many types of data, graphics and large numbers for example, not in Base-2, but in Base-8 or Base-16.

Page 36: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Introducing Octal

Computer scientists are often looking for shortcuts to do things. One of the ways in which we can represent Base-2 numbers is to use their octal (Base-8) equivalents.

This is especially helpful when we have to do fairly complicated tasks using large numbers.

Page 37: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Introducing Octal

The octal numbering system includes eight base digits, 0-7.

After 7, the next placeholder to the right begins with a 1:0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13 ...

Page 38: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Octal DigitsBase-2 Base-8 Base-10

000 0 0

001 1 1

010 2 2

011 3 3

100 4 4

101 5 5

110 6 6

111 7 7

Click to Return to the Last Slide ViewedClick to Return to the Last Slide Viewed

Page 39: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Octal Placeholders

““ Ones

On

es ””

““ Eig

hts

Eig

hts ””

““ Six

ty-

Six

ty-

Fou

rs”

Fou

rs”

64*264*2 8*48*4 1*11*1

8822*2*2 8811*4*4 8800*1*1

22 44 11Number:Number:

Placeholder Placeholder Name:Name:

Value:Value:

Exponential Exponential Expression:Expression:

Page 40: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting Base-2 to Base-8

STEP ONE: Take the binary number and from right to left, group all placeholders in triplets. Add leading zeros, if necessary:

100011001010012

010 001101100001Click to See Octal Digits TableClick to See Octal Digits Table

Page 41: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting Base-2 to Base-8

STEP TWO: Convert each triplet to its single-digit octal equivalent. (HINT: For each triplet, the octal conversion is the same as converting to a decimal number):

100011001010012

010 001101100001

2 1 154

214518

=

Page 42: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting Base-8 to Base-2

Take each octal digit and convert each digit to a binary triplet. Insert leading zeros, if needed:

435208

100 0000101010114 3 025

1000111010100002

=

Click to See Octal Digits TableClick to See Octal Digits Table

Page 43: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting Base-8 to Base-10

Multiply each octal digit by the exponential expression that represents its placeholder and then add the values:

23748 =

N10

2*82*833 = 2*512 = 1024 = 2*512 = 1024 8

3*83*822 = 3*64 = 192 = 3*64 = 192 8

7*87*811 = 7*8 = 56 = 7*8 = 56 8 4*84*800 = 4*1 = 4 = 4*1 = 4 8

= 1276= 12761010

Page 44: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting Base-10 to Base-8

To convert Base-10 to Base-8, we’ll use successive division …

Our new problem:

483210 = ?8

Page 45: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting Base-10 to Base-8

STEP ONE: Draw a table with three columns. Label the last two columns quotient and remainder, respectively.

Q R

Page 46: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting Base-10 to Base-8

STEP TWO: In the first available left-most column, put the expression 4832/8. Calculate the division and put the quotient and remainder in their respective columns.

Q R

4832/84832/8 604604 00

Page 47: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting Base-10 to Base-8

STEP THREE: Take the quotient of the previous expression and divide it by eight. Repeat until you’ve have 0 for a quotient.

Q R

4832/84832/8 604604 00

604/8604/8 7575 44

75/875/8 99 33

9/89/8 11 11

1/81/8 00 11

Page 48: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting Base-10 to Base-8

STEP FOUR: Read the REMAINDER column from the bottom to the top. The numbers in the remainder column represent the octal number.

Q R

4832/8 604 0

604/8 75 4

75/8 9 3

9/8 1 1

1/8 0 1

Page 49: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting Base-10 to Base-8

483210 = 113408

Q R

4832/8 604 0

604/8 75 4

75/8 9 3

9/8 1 1

1/8 0 1

Page 50: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Base-16

What’s tricky about Base-16? This time, we are converting to a base

that has more base digits than Base-10 (Base-2 and Base-8 have fewer digits than Base-10)

So what?

Page 51: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Hexadecimal Numbering

There are new symbols for the Base-16 equivalents of the Base-10 numbers 10, 11, 12, 14 and 15. Examine:

DEC 0 1 2 3 4 5 6 7

HEX 0 1 2 3 4 5 6 7

DEC 8 9 10 11 12 13 14 15

HEX 8 9 A B C D E F

Page 52: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting Base-10 to Base-16

To convert Base-10 to Base-16, we’ll again use successive division …

Our new problem:

21410 = ?16

Page 53: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting Base-10 to Base-16

STEP ONE: Draw a table with three columns. Label the last two columns quotient and remainder, respectively.

Q R

Page 54: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting Base-10 to Base-16STEP TWO: In the first

column, write the expression 214/16. Calculate the expression, taking care not to divide past the decimal point. Write the quotient and remainder in their respective columns.

Q R

214/16 13 6

Page 55: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting Base-10 to Base-16

STEP THREE: Keep bringing down the quotient of the previous expression and dividing it by 16 until you have a quotient of zero.

Q R

214/16 13 6

13/16 0 13

Page 56: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting Base-10 to Base-16

STEP FOUR: Convert all remainders to their hex equivalents.

Q R

214/16 13 6 = 6

13/16 0 13 = D

Page 57: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting Base-10 to Base-16

STEP FIVE: Read the numbers in the REMAINDER column from the bottom up to get your hex equivalent.

Q R

214/16 13 6 = 6

13/16 0 13 = D

2142141010 = D6 = D61616

Page 58: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Binary Arithmetic

Computer processing requires a little more capability in binary math, notably arithmetic.

Let’s consider addition and subtraction …

Page 59: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Binary Addition

Just a few combinations are possible: 02 + 02 = 02

02 + 12 = 12

12 + 02 = 12

What about 12 + 12?

Page 60: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Binary Addition

12 + 12 = 102

Remember what that 10 means: it means a zero in the right column, and a one in the left column

When you carry in base two, you are bringing a power of two to the left…

Page 61: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Binary Addition

11102

+10112

10

1+

0

1+

112

=1410

=1110

=2510

Page 62: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Binary Subtraction

Subtraction in base two is similar to addition 112 – 112 = 002

112 – 002 = 112

102 – 002 = 102

What about 102 - 12?

Page 63: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Binary Addition

102 - 12 = 12 Think about borrowing in decimal – you are really borrowing a

power of your base … In Base-10, we borrow 10 from the left column and give it to the

next column to the right. So … In Base-2, we borrow 2 from the left column and give it to the

next column to the right.

Page 64: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Binary Addition

001100112

-000101102

10

11

1

001

110002

=5110

=2210

=2910

Page 65: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Binary Borrowing: A Little Trick

Many students find it convenient to just mentally convert the binary number to decimal, complete the subtraction, and then convert the answer back to binary.

Consider 102 – 12 = ? A one in binary is still a one in decimal When you borrow for the zero, you bring over a power of

two, which is equal to two The decimal conversion for the borrowing is 2-1, which is

1 And 110 = 12

Be sure to add your answer back up as a check

Page 66: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Negative Numbers

We have mastered binary math for positive integers

But what about negative numbers? Handling the negative sign has proven

somewhat problematic, as we will see …

Page 67: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Negative Integers: Sign/Magnitude Notation

The first solution for encoding negative numbers in binary form was to dedicate a switch (which we now know is a memory location) to communicate whether a number was positive or negative

The only thing required was to agree, in the bank of switches representing the number, which switch referred to the sign

In one of the more popular encoding notations for negative numbers, the first switch is the sign.

Page 68: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Negative Integers: Sign/Magnitude Notation A 0 setting means the number is positive, a 1 setting means the

number is negative. This encoding scheme (for signed integers) is called

sign/magnitude notation. The first switch is the sign, the remaining switches represent the

binary magnitude of the number. Which highlights one of our key concepts about binary encoding

– the leftmost digit could be a negative sign, or a “one” in magnitude

The computer must be told which interpretation/decoding scheme to use

Page 69: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Sign/Magnitude: The Problem

What was wrong with this scheme? Great for all numbers except 0 … This encoding scheme allows you to come up with 2

distinct encoding schemes for 0, one with a positive sign, and one with the negative sign

The one with the negative sign has no mathematical meaning, and the ambiguity of two patterns representing the same number is problematic.

So, most computers use a different scheme than sign/magnitude for representing negative integers …

Page 70: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Twos Complement

Twos Complement solves the ambiguity problem.

The scheme works because we are in binary math, which has only 2 digits.

Offsetting one moves you to the only other possible digit, etc.

Page 71: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Twos Complement

In Twos Complement, you encode positive integers normally.

To use Twos Complement for negatives: Complement every digit in the number (change 1s

to 0s and 0s to 1s) Add 1 to the complemented number

Example:–101 = 010 + 1 = 011

Page 72: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Twos Complement: Math

Twos Complement allows us to add the representation and processing of negative integers to our growing computing capability.

To add a positive and negative number, first perform the twos complement trick on the negative number, and then add normally.

To add two negative numbers, first encode both of them in 2’s complement, and then add normally.

Page 73: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

What about fractional values?

Our goal is to learn how to encode all numbers in a way that they can be represented using switch settings We can encode positive integers We can encode negative integers

But what about decimal numbers (numbers with fractional values)?

To store decimal numbers, most computers utilize scientific notation …

Page 74: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Binary Math and Scientific Notation

As a civilization, we now work with numbers so big and so small that mathematicians have already had to wrestle with creating a manageable notation.

Scientists developed scientific notation as a standard way to represent numbers.

Page 75: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Binary Math and Scientific Notation

Consider the following example:+/-5.334 * 10+/-23

The 5.334 part is the mantissa. The 10 part is the base. The 23 part is the exponent.

Page 76: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Scientific Notation

Let’s move up a level of abstraction to the more generalized case of scientific notation:

+/- M * B+/-E

M is the mantissa. B is the base. E is the exponent. +/- means the number can be positive or negative.

Page 77: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Switch Math: the Holes

If we can figure out how to use scientific notation to encode base ten decimal numbers in a binary form of scientific notation, we can represent any finite number in our computer and our switch math will be complete …

Page 78: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Interpreting Scientific Notation

It turns out that handling scientific notation, or any other encoding scheme, is amazingly simple We design the computer to know which

interpretation scheme to use! We will just figure out how to program in some logic

that says “if a number is tagged as being in scientific notation, just use a look up chart to interpret the number.”

Page 79: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Interpreting Scientific Notation

Given an encoded number, to decode it such that it is in scientific notation, you would need to know several things – How big is the overall memory allocation (how

many switches were used)? How many switches are dedicated to the mantissa,

and which are they? How many switches are dedicated to the exponent,

and which are they?

Page 80: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

One Last Thing!

Okay, so we know we will always need to know the encoding scheme to interpret scientific notation.

But, once we know the encoding scheme for any given computer, we need to be able to encode in this scientific notation form …

Page 81: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Fractional Value Numbers

Consider the decimal (base 10) number: +5.75 How can we encode this is switches? We already know how to do half of the encoding – the

integer part (the numbers to the left of the decimal point). Let’s break the problem into two parts, the left hand side

(of the decimal point) and the right hand side …

Page 82: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Left Side of a Decimal Number

Okay, our number is 5.75 To convert the 510 to ?2:

Use successive division

Division Remainder

5 / 2 = 2 1

2 / 2 = 1 0

1 / 2 = 0 1

551010 = 101 = 10122

Page 83: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Right Side of a Decimal Number On the left hand side, we divided successively

by 2 to convert On the right hand side, we multiply successively

by 2 to convert We are multiplying numbers by 2 to “promote” them

to the left hand side of the decimal place. Once promoted, we record them as a digit in our

answer. This process is called extraction….

Page 84: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Extraction

STEP ONE: Draw a table with two columns. Label the columns for the expression and the extraction, respectively.

Expression Extract.

Page 85: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Extraction

STEP TWO: Write down your original number and multiply it by 2. Put the whole number part in the Extraction column.

Expression Extract.

0.75 * 2 = 1.50 1

Page 86: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Extraction

STEP THREE: Bring down the fractional part (the number to the right of the decimal point) and multiply it by two. Extract the whole number, as before.Repeat until you extract 0.

Expression Extract.

0.75 * 2 = 1.50 1

0.50 * 2 = 1.00 1

0.00 * 2 = 0.00 0

Page 87: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Extraction

STEP FOUR: Read the extraction column, from top to bottom. This number represents your binary equivalent.

Expression Extract.

0.75 * 2 = 1.50 1

0.50 * 2 = 1.00 1

0.00 * 2 = 0.00 0

0.750.751010 = 110 = 11022

Page 88: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Both Sides Now

5.7510 = 101.1102 This isn’t in scientific notation yet, but at least we have figured out how to convert it to binary

Recall the steps so far: Do the problem in halves For the left side of the decimal, use successive

division by 2 and read the remainders from bottom to top

For the right hand side of the decimal, use successive multiplication by 2 and read the extractions from top to bottom

Page 89: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Let’s Try One More …

Let’s try another problem. This time, with a number that doesn’t convert to binary as easily …

Let’s try this number:

2.5410 = ?2

Page 90: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Converting

Remember our algorithm: left side by successive division, right side by successive multiplication

Page 91: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Left Side of 2.54

2 by successive divisionDivision Remainder

2/2 = 1 0

1/0 = 0 1

Reading the remainders from bottom to top, 210 = 102

Page 92: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Right Side of 2.54

• Reading the extraction column, top to bottom:

.5410 = 1000101002

What would have if we kept extracting, past .24 * 2?

Let’s convert our number back to Base-10 to see what happens …

Division Extraction

.54 * 2 = 1.08 1

.08 * 2 = 0.16 0

.16 * 2 = 0.32 0

.32 * 2 = 0.64 0

.64 * 2 = 1.28 1

.28 * 2 = 0.56 0

.56 * 2 = 1.12 1

.12 * 2 = 0.24 0

.24 * 2 = 0.48 0

Page 93: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Let’s Go Back the Other Way

Putting 10.1000101002 back to base 10

Left hand side first, expanded notation 102 = (0 * 20) + (1 * 21) = 0 + 2 = 210

Now, to convert the right hand side…

Page 94: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Right Side of a Decimal Number

Remember, the left hand side represents powers of two … positive powers of two

Moving left from the decimal point, we saw the unit values were 20, 21, 22, 23, 24, etc.

Doesn’t it seem reasonable that the units to the right of the decimal represent negative powers of two?

Moving from the right of the decimal point, we will see unit values for 2-1, 2-2, 2-3, 2-4, 2-5, etc.

Page 95: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Okay, But What Does That Mean?

Exactly what is a number to a negative power, such as 2-

1, 2-2, 2-3? These are numbers you

already know we are just used to seeing them written in a different format:

2 –1 1/21 ½ .5

2 –2 1/22 ¼ .25

2 –3 1/23 1/8 .125

2-4 1/24 1/16 .0625

Page 96: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Back to Our Conversion …

Remember, we are translating 10.100010100 We finished the left hand side = 102 = 210

Now for the right hand side: .100010100 =

(1 * 2-1) + (0* 2-2) + (0* 2-3) + (0* 2-4) + (1* 2-5) + (0* 2-6) +

(1* 2-7) + (0* 2-8) + (0* 2-9) Which is equal to ½ + 1/32 + 1/128 (we can throw out the zero

terms) Which is equal to 69/128 = .539

Combining both sides, 10.1000101002 = 2.53910

But we started with 2.54! Why did we get the error?

Page 97: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

The Imprecision of Conversions We started with 2.5410, took it to switch units in

binary, and then back to decimal Our result was 2.539 This is a precision error:

2.539 / 2.540 = .99, so our percentage error is 1%!!!

As you might imagine, in large, multi-step calculations these errors can become significant enough for you to see if you are working in a package such as Excel….

Page 98: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Storing Binary in Scientific Notation

Remember, we said if we could encode a decimal number in switch math, we were good to go.

Decimal numbers are usually stored in a binary version of scientific notation.

If we can take our translation of 2.54, and store it in a binary version of scientific notation, we can quit!

Page 99: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Last Step

There are several switch encoding schemes for scientific notation.

The good news, remember, is that you (and the computer!) will always have to be told which encoding scheme to use.

One common encoding scheme is 16-bit encoding …

Page 100: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

16 Bit Scientific Notation Encoding

In 16 bit encoding, 16 bits (or switches) are allocated to the numeric part of a number expressed in scientific notation.

There are a few different flavors of 16 bit encoding, with a different allocation of switches between the mantissa and the exponent.

Page 101: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

One 16 Bit Encoding in Detail

Remember the parts of a number in scientific notation? – the mantissa, base and exponent?

Each is assigned to specific locations in the 16 bit switch pattern. In the variation we are looking at first: Switch 1 = Sign of the mantissa (Decimal point assumed to go next, but isn’t stored) Switches 2-10 – next 9 switches are for the mantissa Switch 11 – Sign of the exponent Switches 12-16 – next 5 switches are for the exponent

Page 102: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

16 Bit: More

What happened to the base? We don’t have to store that, because it will always be base two, the binary base from the world of switches

Don’t need to store the decimal point, because the encoding scheme will guarantee it is placed correctly when decoded

There’s only one problem: our number must be “normalized” before encoding This means that we need to adjust the exponent as

required to insure that the first significant digit is the first digit to the right of the decimal point

Page 103: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Normalizing

Take the number 101.112, which is really 101.11 * 20

Before we can store this number in scientific notation, we must normalize it.

We have to adjust the exponent until the first significant digit, which is the leftmost 1, is the first digit to the right of the decimal point.

In other words, we have to move the decimal place in our example 3 places to the left, so that the number become .10111.

Page 104: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Normalizing

How can we legally do this, without changing the value of the number?

Every time you move the decimal place to the left, you must adjust the exponent by a positive increment.

In our case, we moved 3 decimal points to the left, so our new exponent is 23

In other words, 101.11 * 20 = .10111 * 23

Page 105: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Normalizing Examples

A couple more, to make sure we have it: 1101.11 * 20 = .110111 * 24

101.1 * 20 = .1011 * 23

.01 * 20 = .1 * 2-1

.001 * 20 = .1 * 2-2

Page 106: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Algorithm

1. Convert the base ten number to base two, one side of the decimal place at a time Left hand side, successive division Right hand side, successive multiplication

In our case, 2.5410 = 10.1000101002

Page 107: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Algorithm (con’t)

Put number in normalized form10.100010100 * 20 = .10100010100 * 22

Determine what the encoding scheme is For the sake of our problem , assume the

following 16 bit encoding: 1 = Sign of mantissa 2-12 = Mantissa 13 = Sign of exponent 14-16 = Exponent

Page 108: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Algorithm (con’t)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

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

Using the described notation system, the decimal 2.5410 would be stored in switch binary math as the following pattern of zeros and ones:

0101000101000010

Page 109: Number Systems and Binary Math. Why do computers use switches?  Why did engineers select a binary-state (two state) switch as the fundamental computer

Questions?