23
ITEC 352 Lecture 7 Binary(2)

ITEC 352 Lecture 7 Binary(2). Review Homework due on Friday Questions Binary Addition Subtraction Encoding

Embed Size (px)

Citation preview

ITEC 352

Lecture 7Binary(2)

Binary (2)

Review

• Homework due on Friday• Questions• Binary• Addition• Subtraction• Encoding

Binary (2)

Outline

• Two’s complement• Excess notation• Floating point format

Binary (2)

Two’s complement

• Similar to one’s complement• Solves issues– No double 0– Addition / subtraction are the same

• Small conversion cost– Copy all 0’s from right to left– Copy first one– Flip all other bits

Binary (2)

Class discussion

• Represent the following in one’s complement and 2’s complement:

-9-779

Binary (2)

Excess Representation

• Which one of the following two numbers represented in 1’s complement is greater? 1011 (or) 0100

What are the steps you had to go through to figure this out?

We can avoid these steps by using an excess representation.

Binary (2)

Excess representation

Key idea: Make all negative numbers positive by shifting them up ….

E.g., Suppose we want to represent numbers from -4 to +3. How do we make all negative numbers positive but at the same time

preserving the difference in magnitudes between 2 numbers?

We could do this by adding “+4” to all the numbers. Therefore, -4 becomes 0 -3 becomes 1 … 0 becomes 4 … 3 becomes 7Because we shifted the numbers by 4, we call this Excess 4

representation

Binary (2)

Excess Representation

• How does this help? • Well consider, the question we asked earlier: Which

one of the following two numbers is greater? 0000 or 0011 ?

“0000” in Excess 4 representation is -4. 0011 is -1. Comparing numbers becomes easier when Excess

notation is used.

Binary (2)

Excessiveness

• The “bias” or “excess” (e.g., 4 in the previous slide) can be any number. Usually the bias is the smallest number that we would like to represent.

• E.g., if we want to represent numbers from -128 to +127, we will use Excess 128 representation. Because we need to consider “0”, we are stopping at 127 and not 128.

Binary (2)

SUMMARY: Representing negative numbers in a 3 bit architecture (example)

Binary (2)

Real numbers

• Numbers of the form: 101.1110101101101011001010101101010

. Contain a fractional part.

Why even bother with real numbers? Isn’t this some esoteric stuff that is never used??

Binary (2)

Rationale

• Floating point computations are everywhere – more common than simple integer computations.

• Examples: – Any engineering discipline: e.g., Construction industry– Computer security (e.g., random number generation)– Computer games (several graphical computations)

• Closer to reality: – My experience: NUMBER 1 reason why student

programs don’t work in Programming contests!!

Binary (2)

Real number problem

• How to store infinitely many floating point numbers into a finite number of bits.

• Consider a 34 digit binary number: 101.1110101101101011001010101101010How can we store this in a computer whose

bus width is 32?

Binary (2)

Real number Numbers

• Consider a 34 digit binary number: 101.1110101101101011001010101101010How can we store this in a computer whose bus width is 32? Option 1: Split it into two parts (2 X 32-bit integers).

Problem: Slows handling of arithmetic operations. Why?Option 2: Sacrifice some precision.

This is the idea behind a real number representation called floating point numbers

Can you come up with your own standard?

Binary (2)

Standards

• Come up with a standard such that the numbers:

10.10110.100

Can be represented within 4 bits.

Can your standard now represent: 100.11

Binary (2)

Real numbers

• A common way of representing real numbers is using “floating point numbers”.

• E.g., – real number: 352.452 – Floating point number representations:

3.52452 * 102, or

35.2452 * 101, or 0.352452 * 103

Binary (2)

Floating point

– A real number: 352.452 can have manyFloating point number

representations: 3.52452 * 102, or

35.2452 * 101, or 0.352452 * 103

• To standardize the position of the floating point – it is normally represented as: – 3.52452 * 102 (only one digit to the left

of the floating point).

Binary (2)

Examples

• Normalize the following numbers in base 10:2345.452

How about in base 2: 101.11101111.0111

Binary (2)

Base 10

More terms:Precision: number of digits in the significand

-- sometimes fractions are too large – we need to approximate. The precision tells us by how much.

Range: Number of digits in the exponent. • Example Consider the number: 60234.45

if precision is 4 and range is 2 then this number is represented as:

+(6.023 X 1023):

Binary (2)

Base 2 Floating Point

Consider the binary numbers:

10.10110.100

0.1001

What is their normalized representation if precision = 4 and range = 2?

Do you see any error?

Binary (2)

Base 2 Floating Point Numbers

Consider the binary numbers:

10.10110.100

What is their representation if precision = 4 and range = 2?

Do you see any error?

Error is called the rounding error. Every programming language suffers from this.

Binary (2)

How to live with error.

• Floating point computations result in rounding errors (precision errors).

• However, the key is: be consistent with errors.

• For this, every language/architecture must follow one standard (so that the error is repeatable).–Most famous standard = IEEE 754

standard

Binary (2)

Summary

• Two’s complement• Excess notation• Buildup to floating point numbers