Download - Computer Organization and Design Information Encoding II Montek Singh Wed, Aug 29, 2012 Lecture 3
Computer Organization and Computer Organization and DesignDesign
Information Encoding IIInformation Encoding II
Montek SinghMontek Singh
Wed, Aug 29, 2012Wed, Aug 29, 2012
Lecture 3Lecture 3
Today’s topicsToday’s topics Positive and Negative numbersPositive and Negative numbers Non-IntegersNon-Integers 0
00
0
1
11
1
Review: Sign-Magnitude Rep.Review: Sign-Magnitude Rep. What about What about signedsigned numbers? numbers?
one obvious idea: use an extra bit to encode the signone obvious idea: use an extra bit to encode the signconvention: the most significant bit (leftmost) is used for convention: the most significant bit (leftmost) is used for
the signthe signcalled the SIGNED MAGNITUDE representationcalled the SIGNED MAGNITUDE representation
S 21029 28 27 26 25 24 23 22 21 20
011111010000
2000
1
-2000
Review: Sign-Magnitude Rep.Review: Sign-Magnitude Rep. The Good: Easy to negate, find absolute valueThe Good: Easy to negate, find absolute value The Bad:The Bad:
add/subtract is complicatedadd/subtract is complicateddepends on the signsdepends on the signs4 different cases!4 different cases!
two different ways of representing a 0two different ways of representing a 0 it is not used that frequently in practiceit is not used that frequently in practice
except in floating-point numbersexcept in floating-point numbers
Alternative: 2Alternative: 2’’s Complement Rep.s Complement Rep.
20212223…2N-2-2N-1 ……
N bits
The 2’s complement representation for signed integers is the most commonly used signed-integer representation. It is a simple modification of unsigned integers where the most significant bit is considered negative.
“binary” point“sign bit”Range: – 2N-1 to 2N-1 – 1
8-bit 2’s complement example:
11010110 = –27 + 26 + 24 + 22 + 21 = – 128 + 64 + 16 + 4 + 2 = – 42
ladderschute
Why 2Why 2’’s Complement?s Complement? Benefit: the same binary addition mod 2Benefit: the same binary addition mod 2nn
procedure will work for adding positive and procedure will work for adding positive and negative numbers negative numbers dondon’’t need separate subtraction rules!t need separate subtraction rules! The same procedure will also handle unsigned The same procedure will also handle unsigned
numbers!numbers! Example: 5510 = 001101112
+ 1010 = 000010102
6510 = 010000012
5510 = 001101112
+ -1010 = 111101102
4510 = 1001011012
When using signed magnitude representations, adding a negative value really means to subtract a positive value. However, in 2’s complement, adding is adding regardless of sign. In fact, you NEVER need to subtract when you use a 2’s complement representation.
22’’s Complements Complement How to negate a number?How to negate a number?
First complement every bit (i.e. 1 First complement every bit (i.e. 1 0, 0 0, 0 1), then 1), then add 1add 1Example: 20 = 00010100, -20 = 11101011 + 1 = Example: 20 = 00010100, -20 = 11101011 + 1 =
1110110011101100Example: -20 = 11101100, 20 = 00010011 + 1 = Example: -20 = 11101100, 20 = 00010011 + 1 =
0001010000010100
Why does this work?Why does this work?You figure it out! Hint: You figure it out! Hint:
22’’s Complements Complement Sign-ExtensionSign-Extension
suppose you have an 8-bit number that needs to be suppose you have an 8-bit number that needs to be “extended” to 16 bits“extended” to 16 bitsWhy? Maybe because we are adding it to a 16-bit Why? Maybe because we are adding it to a 16-bit
number…number…
ExamplesExamples16-bit version of 42 = 16-bit version of 42 = 0000 0000 0000 0000 0010 10100010 10108-bit version of -2 = 1111 11108-bit version of -2 = 1111 1110
Why does this work?Why does this work?Same hint:Same hint:
1111 1111
CLASS EXERCISECLASS EXERCISE
10’s-complement Arithmetic (You’ll never need to borrow again)
Step 1) Write down two 3-digit numbers that you want to subtract
Step 2) Form the 9’s-complement of each digitin the second number (the subtrahend)
0 91 82 73 64 55 46 37 28 19 0
Helpful Table of the9’s complement for
each digit
Step 3) Add 1 to it (the subtrahend)
Step 4) Add this number to the first
What did you get? Why weren’t you taught to subtract this way?
Step 5) If your result was less than 1000, form the 9’s complement again and add 1 and remember your result is negativeelse subtract 1000
Tutorial on Base Conversion Tutorial on Base Conversion (+ve (+ve ints)ints) Binary to DecimalBinary to Decimal
multiply each bit by its positional power of 2multiply each bit by its positional power of 2 add them togetheradd them together
Decimal to BinaryDecimal to Binary Problem: given Problem: given v,v, find find bbii (inverse problem) (inverse problem) Hint: expand seriesHint: expand series
observe: every term is even except firstobserve: every term is even except first– this determines this determines bb00– divide both sides by 2divide both sides by 2
Tutorial on Base Conversion Tutorial on Base Conversion (+ve (+ve ints)ints) Decimal to BinaryDecimal to Binary
Problem: given Problem: given v,v, find find bbii (inverse problem) (inverse problem)
Algorithm:Algorithm:RepeatRepeat
– divide divide vv by 2 by 2
– remainder becomes the next bit, remainder becomes the next bit, bbii– quotient becomes the next quotient becomes the next vv
Until Until vv equals 0 equals 0
Note: Same algorithm applies to other number Note: Same algorithm applies to other number basesbases just replace divide-by-2 by divide-by-just replace divide-by-2 by divide-by-nn for base for base nn
Non-Integral NumbersNon-Integral Numbers How about non-integers?How about non-integers?
examplesexamples1.2341.234 -567.34-567.340.000010.000010.00000000000000120.0000000000000012
fixed-point representationfixed-point representation floating-point representationfloating-point representation
Fixed-Point RepresentationFixed-Point Representation Set a definite position for the “binary” pointSet a definite position for the “binary” point
everything to its left is the integral part of the numbereverything to its left is the integral part of the number everything to its right is the fractional part of the everything to its right is the fractional part of the
numbernumber
1101.01101101.0110 = 2= 233 + 2 + 222 + 2 + 200 + 2 + 2-2-2 + 2 + 2-3-3
= 8 + 4 + 1 + 0.25 + = 8 + 4 + 1 + 0.25 + 0.1250.125
= 13.375= 13.375
Or1101.0110 = 214 * 2-4 = 214/16 = 13.375
23 22 21 20 2-1 2-2 2-3 2-4
Fixed-Point Base ConversionFixed-Point Base Conversion Binary to DecimalBinary to Decimal
multiply each bit by its positional power of 2multiply each bit by its positional power of 2 just that the powers of 2 are now negativejust that the powers of 2 are now negative for m fractional bitsfor m fractional bits
ExamplesExamples 0.10.122 = ½ = 0.5 = ½ = 0.51010
0.00110.001122 = 1/8 + 1/16 = 0.1875 = 1/8 + 1/16 = 0.18751010
0.0011001100110.00110011001122= = 1/8+1/16+1/128+1/256+1/2048+1/4096 = 1/8+1/16+1/128+1/256+1/2048+1/4096 = 0.199951171870.199951171871010 (getting close to 0.2) (getting close to 0.2)
0.00110.001122 (repeats) = 0.2 (repeats) = 0.21010
Fixed-Point Base ConversionFixed-Point Base Conversion Decimal to BinaryDecimal to Binary
Problem: given Problem: given v,v, find find bbii (inverse problem) (inverse problem)
Hint: this time, try multiplying by 2Hint: this time, try multiplying by 2
whole number part is whole number part is bb-1-1 remaining fractional part is the restremaining fractional part is the rest
Algorithm:Algorithm:RepeatRepeat
– multiply multiply vv by 2 by 2
– whole part becomes the next bit, whole part becomes the next bit, bbii– remaining fractional part becomes the next remaining fractional part becomes the next vv
Until (Until (vv equals 0) or (desired accuracy is achieved) equals 0) or (desired accuracy is achieved)
Repeated Binary FractionsRepeated Binary Fractions Not all fractions have a finite representationNot all fractions have a finite representation
e.g., in decimal, 1/3 = 0.3333333… (unending)e.g., in decimal, 1/3 = 0.3333333… (unending)
In binary, many of the fractions you are used In binary, many of the fractions you are used to have an infinite representation!to have an infinite representation! ExamplesExamples
1/10 = 0.11/10 = 0.11010 = 0.000110011… = 0.000110011…22=0.0=0.00011001122
1/5 = 0.21/5 = 0.21010 = 0. = 0.001100112 2 = 0.333…= 0.333…1616
QuestionQuestion In Decimal: When do fractions repeat?In Decimal: When do fractions repeat?
when the denominator is mutually prime w.r.t. 5 and 2when the denominator is mutually prime w.r.t. 5 and 2 In Binary: When do fractions repeat?In Binary: When do fractions repeat?
when the denominator is mutually prime w.r.t. 2when the denominator is mutually prime w.r.t. 2 i.e., when denominator is anything other than a power of 2i.e., when denominator is anything other than a power of 2
Signed fixed-point numbersSigned fixed-point numbers How do you incorporate a sign?How do you incorporate a sign?
use sign magnitude representationuse sign magnitude representationan extra bit (leftmost) stores the signan extra bit (leftmost) stores the sign just as in negative integersjust as in negative integers
2’s complement2’s complement leftmost bit has a negative coefficientleftmost bit has a negative coefficient
1101.0110 = -21101.0110 = -233 + 2 + 222 + 2 + 200 + 2 + 2-2-2 + 2 + 2-3-3
= -8 + 4 + 1 + 0.25 + 0.125 = -2.625= -8 + 4 + 1 + 0.25 + 0.125 = -2.625OR:OR:
– first ignore the binary point, use 2’s complement, put the first ignore the binary point, use 2’s complement, put the point backpoint back
1101.0110 = (-128 + 64 + 16 + 4 + 2)* 21101.0110 = (-128 + 64 + 16 + 4 + 2)* 2-4-4
= -42/16 = -2.625= -42/16 = -2.625
-23 22 21 20 2-1 2-2 2-3 2-4
Bias NotationBias Notation Idea: add a large number to everything, to Idea: add a large number to everything, to
make everything look positive!make everything look positive! must subtract this “bias” from every representationmust subtract this “bias” from every representation This representation is called This representation is called ““Bias NotationBias Notation””..
1 1 0 1 0 1 1 02025 24 23 22 212627
Ex: (Bias = 127)6 * 1 = 6
13 * 16 = 208 - 127 87
Why? Monotonicity
Floating-Point RepresentationFloating-Point Representation Another way to represent numbers is to use a Another way to represent numbers is to use a
notation similar to Scientific Notation.notation similar to Scientific Notation. This format can be used to represent numbers with This format can be used to represent numbers with
fractions (3.90 x 10fractions (3.90 x 10-4-4), very small numbers (1.60 x 10), very small numbers (1.60 x 10--
1919), and large numbers (6.02 x 10), and large numbers (6.02 x 102323).). This notation uses two fields to represent each This notation uses two fields to represent each
number. The first part represents a normalized number. The first part represents a normalized fraction (called the significand), and the second part fraction (called the significand), and the second part represents the exponent (i.e. the position of the represents the exponent (i.e. the position of the ““floatingfloating”” binary point). binary point).
ExponentFractionNormalized 2
Normalized Fraction
“dynamic range” “bits of accuracy”
Exponent
IEEE 754 Format
1
S
1 11
S
52
SignificandExponent
23
Significand
This is effectively a signed magnitude fixed-point number with a “hidden” 1.
The 1 is hidden because it provides no information after the number is “normalized”
8
Exponent
The exponent is represented in bias 127 notation. Why?
Single-precision format
Double-precision format
IEEE 754 Floating-Point FormatsIEEE 754 Floating-Point Formats
SummarySummary Selecting the encoding of information has Selecting the encoding of information has
important implications on how this information important implications on how this information can be processed, and how much space it can be processed, and how much space it requires.requires.
Computer arithmetic is constrained by finite Computer arithmetic is constrained by finite representations, this has advantages (it allows representations, this has advantages (it allows for complement arithmetic) and disadvantages for complement arithmetic) and disadvantages (it allows for overflows, numbers too big or (it allows for overflows, numbers too big or small to be represented). small to be represented).
Bit patterns can be interpreted in an endless Bit patterns can be interpreted in an endless number of ways, however important standards number of ways, however important standards do existdo exist TwoTwo’’s complements complement IEEE 754 floating pointIEEE 754 floating point