Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

24
Computer Organization and Computer Organization and Design Design Information Encoding Information Encoding Montek Singh Montek Singh Mon, Jan 12, 2011 Mon, Jan 12, 2011 Lecture 2 Lecture 2

description

Motivations  Computers Process Information  Information is measured in bits  By virtue of containing only “switches” and “wires” digital computer technologies use a binary representation of bits  How do we use/interpret bits?  We need standards of representations for Letters Letters Numbers Numbers Colors/pixels Colors/pixels Music Music Etc. Etc. Today

Transcript of Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

Page 1: Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

Computer Organization and Computer Organization and DesignDesign

Information EncodingInformation Encoding

Montek SinghMontek SinghMon, Jan 12, 2011Mon, Jan 12, 2011

Lecture 2Lecture 2

Page 2: Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

Representing InformationRepresenting Information ““Bit Juggling”Bit Juggling”

Representing information Representing information using bitsusing bits

Number representationsNumber representations

Reading: Chapter 2.3-2.4Reading: Chapter 2.3-2.4

0

00

0

1

11

1

Page 3: Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

MotivationsMotivations Computers Process InformationComputers Process Information Information is measured in bitsInformation is measured in bits By virtue of containing only “switches” and By virtue of containing only “switches” and

“wires” digital computer technologies use a “wires” digital computer technologies use a binary representation of bitsbinary representation of bits

How do we use/interpret bits?How do we use/interpret bits? We need standards of representations forWe need standards of representations for

LettersLettersNumbersNumbersColors/pixelsColors/pixelsMusicMusicEtc.Etc.

Today

Page 4: Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

EncodingEncoding Encoding describes the process of assigning Encoding describes the process of assigning

representations to informationrepresentations to information Choosing an appropriate and efficient Choosing an appropriate and efficient

encoding is a real engineering challenge (and encoding is a real engineering challenge (and an art)an art)

Impacts design at many levelsImpacts design at many levelsMechanism (devices, # of components used)Mechanism (devices, # of components used)Efficiency (bits used)Efficiency (bits used)Reliability (noise)Reliability (noise)Security (encryption)Security (encryption)

Page 5: Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

Fixed-Length EncodingsFixed-Length EncodingsIf all choices are equally likely (or we have no If all choices are equally likely (or we have no

reason to expect otherwise), then a fixed-reason to expect otherwise), then a fixed-length code is often used.length code is often used.Such a code should use at least enough bits to Such a code should use at least enough bits to

represent the information content.represent the information content.ex. Decimal digits 10 = {0,1,2,3,4,5,6,7,8,9}ex. Decimal digits 10 = {0,1,2,3,4,5,6,7,8,9}

4-bit BCD (binary code decimal): 0000 to 10014-bit BCD (binary code decimal): 0000 to 1001

ex. ~84 English characters = {A-Z (26), a-z (26), 0-9 ex. ~84 English characters = {A-Z (26), a-z (26), 0-9 (10), punctuation (8), math (9), financial (5)}(10), punctuation (8), math (9), financial (5)}7-bit ASCII (American Standard Code for Information 7-bit ASCII (American Standard Code for Information

Interchange)Interchange)bits7392.6)84(log2

bits4322.3)10(log2

Page 6: Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

ASCII TableASCII Table

Page 7: Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

UnicodeUnicode ASCII is biased towards western languages, ASCII is biased towards western languages,

esp. Englishesp. English In fact, many more than 256 chars in common In fact, many more than 256 chars in common

use:use:â, m, ö, ñ, è, ¥, â, m, ö, ñ, è, ¥, 揗揗 , , 敇敇 , , 횝횝 , , カカ , ℵ, ℷ, , ℵ, ℷ, жж, , คค

Unicode is a worldwide standard that supports Unicode is a worldwide standard that supports all languages, special characters, classic, and all languages, special characters, classic, and arcanearcaneSeveral encoding variants, e.g. 16-bit (UTF-8)Several encoding variants, e.g. 16-bit (UTF-8)

10x x x x x x10 z y y y y x11110www 10wwz z z z

0x x x x x x xASCII equiv range:

10x x x x x x110y y y y xLower 11-bits of 16-bit Unicode

10x x x x x x10 z y y y y x1110 z z z z16-bit Unicode

Page 8: Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

Encoding Positive IntegersEncoding Positive Integers It is straightforward to encode positive It is straightforward to encode positive

integers as a sequence of bits.integers as a sequence of bits.Each bit is assigned a weightEach bit is assigned a weightWeights are increasing powers of 2, right to leftWeights are increasing powers of 2, right to leftThe value of an n-bit number encoded in this fashion The value of an n-bit number encoded in this fashion

is given by the following formula:is given by the following formula:

v = 2ibii=0

n−1

∑21121029 28 27 26 25 24 23 22 21 20

011111010000 24 = 16

+ 28 = 256

+ 26 = 64+ 27 = 128

+ 29 = 512+ 210 = 1024

200010

Page 9: Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

Some Bit TricksSome Bit Tricks You are going to have to get accustomed to You are going to have to get accustomed to

working in binary.working in binary.Specifically for Comp 411, but it will be helpful Specifically for Comp 411, but it will be helpful

throughout your career as a computer scientist.throughout your career as a computer scientist. Here are some helpful guides Here are some helpful guides

1. Memorize the first 10 powers of 2

20 = 1 25 = 3221 = 2 26 = 6422 = 4 27 = 12823 = 8 28 = 25624 = 16 29 = 512

Page 10: Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

More Tricks with BitsMore Tricks with Bits You are going to have to get accustomed to You are going to have to get accustomed to

working in binary.working in binary.Specifically for Comp 411, but it will be helpful Specifically for Comp 411, but it will be helpful

throughout your career as a computer scientist.throughout your career as a computer scientist. Here are some helpful guides Here are some helpful guides 2. Memorize the prefixes for powers of 2 that are

multiples of 10

210 = Kilo (1024)220 = Mega (1024*1024)230 = Giga (1024*1024*1024)240 = Tera (1024*1024*1024*1024)250 = Peta (1024*1024*1024 *1024*1024)260 = Exa (1024*1024*1024*1024*1024*1024)

Page 11: Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

Even More Tricks with BitsEven More Tricks with Bits You are going to have to get accustomed to You are going to have to get accustomed to

working in binary.working in binary.Specifically for Comp 411, but it will be helpful Specifically for Comp 411, but it will be helpful

throughout your career as a computer scientist.throughout your career as a computer scientist. Here are some helpful guides Here are some helpful guides

3. When you convert a binary number to decimal, first break it down into clusters of 10 bits.

4. Then compute the value of the leftmost remaining bits (1) find the appropriate prefix (GIGA) (Often this is sufficient)

5. Compute the value of and add in each remaining 10-bit cluster

0000101000

0000001100

0000000011

01

Page 12: Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

Other Helpful ClusteringsOther Helpful Clusterings Often it is convenient to cluster groups of bits Often it is convenient to cluster groups of bits

together for a more compact representation.together for a more compact representation.The clustering of 3 bits is called The clustering of 3 bits is called OctalOctal. Octal is not . Octal is not

that common today.that common today.

v = 8id ii=0

n−1

∑21121029 28 27 26 25 24 23 22 21 20

01111101000003720

Octal - base 8000 - 0001 - 1010 - 2011 - 3100 - 4101 - 5110 - 6111 - 7

= 200010

Seems naturalto me!

0273

200010

0*80 = 0

+ 3*83 = 1536

+ 2*81 = 16+ 7*82 = 448

Page 13: Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

One Last ClusteringOne Last Clustering Clusters of 4 bits are used most frequently. Clusters of 4 bits are used most frequently.

This representation is called This representation is called hexadecimalhexadecimal..The hexadecimal digits include 0-9, and A-F, and each The hexadecimal digits include 0-9, and A-F, and each

digit position represents a power of 16.digit position represents a power of 16.

v = 16id ii=0

n−1

∑ 21121029 28 27 26 25 24 23 22 21 20

0111110100000x7d0

Hexadecimal - base 160000 - 0 1000 - 80001 - 1 1001 - 90010 - 2 1010 - a0011 - 3 1011 - b0100 - 4 1100 - c0101 - 5 1101 - d0110 - 6 1110 - e0111 - 7 1111 - f

= 200010

0d7

200010

0*160 = 0 + 13*161 = 208+ 7*162 = 1792

Page 14: Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

Signed-Number RepresentationsSigned-Number Representations There are also schemes for representing There are also schemes for representing

signed integers with bits.signed integers with bits.One obvious method is to encode the sign of the One obvious method is to encode the sign of the

integer using one bit.integer using one bit.Conventionally, the most significant bit is used for the Conventionally, the most significant bit is used for the

sign. This encoding for signed integers is called the sign. This encoding for signed integers is called the SIGNED MAGNITUDE representation.SIGNED MAGNITUDE representation.

S 21029 28 27 26 25 24 23 22 21 20

011111010000

v = −1S 2ibii=0

n−2

∑2000

1

-2000

Anything weird?

∙ The Good: Easy to negate, find absolute value∙ The Bad:

– Add/subtract is complicated; depends on the signs– Two different ways of representing a 0– It is not used that frequently in practice

Page 15: Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

2’s Complement Integers2’s Complement Integers

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

v = −2n−1bn−1 + 2ibii=0

n−2

∑8-bit 2’s complement example:

11010110 = –27 + 26 + 24 + 22 + 21 = – 128 + 64 + 16 + 4 + 2 = – 42

ladderschute

Page 16: Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

Why 2’s Complement?Why 2’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 don’t need separate subtraction rules!don’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.

Page 17: Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

2’s Complement Tricks2’s Complement Tricks Negation – changing the sign of a numberNegation – changing the sign of a number

First complement every bit (i.e. 1 First complement every bit (i.e. 1 0, 0 0, 0 1) 1)Add 1Add 1

Example: 20 = 00010100, -20 = 11101011 + 1 = Example: 20 = 00010100, -20 = 11101011 + 1 = 1110110011101100

Sign-Extension – aligning different sized 2’s Sign-Extension – aligning different sized 2’s complement integerscomplement integers

16-bit version of 42 = 0000 0000 0010 101016-bit version of 42 = 0000 0000 0010 10108-bit version of -2 = 1111 11108-bit version of -2 = 1111 1110

1111 1111

Page 18: Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

CLASS EXERCISECLASS EXERCISE10’s-complement Arithmetic

(You’ll never need to borrow again)Step 1) Write down two 3-digit numbers that you

want to subtractStep 2) Form the 9’s-complement of each digit

in 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

Page 19: Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

Fixed-Point NumbersFixed-Point NumbersBy moving the implicit location of the “binary” By moving the implicit location of the “binary” point, we can represent signed fractions too. point, we can represent signed fractions too. This has no effect on how operations are This has no effect on how operations are performed, assuming that the operands are performed, assuming that the operands are properly aligned.properly aligned.

1101.01101101.0110 = –2= –233 + 2 + 222 + 2 + 200 + 2 + 2-2-2 + 2 + 2-3-3

= – 8 + 4 + 1 + 0.25 + 0.125= – 8 + 4 + 1 + 0.25 + 0.125= – 2.625= – 2.625

Or1101.0110 = -42 * 2-4 = -42/16 = -2.625

-23 22 21 20 2-1 2-2 2-3 2-4

Page 20: Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

Repeated Binary FractionsRepeated Binary Fractions Not all fractions can be represented exactly Not all fractions can be represented exactly

using a finite representation.using a finite representation.You’ve seen this before in decimal notation where the You’ve seen this before in decimal notation where the

fraction 1/3 (among others) requires an infinite fraction 1/3 (among others) requires an infinite number of digits to represent (0.3333…).number of digits to represent (0.3333…).

In Binary, a great many fractions that you’ve In Binary, a great many fractions that you’ve grown attached to require an infinite number grown attached to require an infinite number of bits to represent exactly.of bits to represent exactly.

Ex: 1/10 = 0.1Ex: 1/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

Page 21: Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

Bias NotationBias Notation There is yet one more way to represent signed There is yet one more way to represent signed

integers, which is surprisingly simple. It integers, which is surprisingly simple. It involves subtracting a fixed constant from a involves subtracting a fixed constant from a given unsigned number.given unsigned number.This representation is called “Bias Notation”.This representation is called “Bias Notation”.

v = 2ibii=0

n−1

∑ − Bias 1 1 0 1 0 1 1 02025 24 23 22 212627

Ex: (Bias = 127) 6 * 1 = 613 * 16 = 208

- 127 87

Why? Monotonicity

Page 22: Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

Floating Point NumbersFloating Point Numbers 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 “floating” binary point). “floating” binary point).

ExponentFractionNormalized 2

Normalized Fraction

“dynamic range” “bits of accuracy”

Exponent

Page 23: Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

IEEE 754 Format

1

S

v = −1S ×1.Significand × 2Exponent−127

1 11

S

52

SignificandExponent

v = −1S ×1.Significand × 2Exponent−1023

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

Page 24: Computer Organization and Design Information Encoding Montek Singh Mon, Jan 12, 2011 Lecture 2.

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 (it for complement arithmetic) and disadvantages (it allows for overflows, numbers too big or small to allows for overflows, numbers too big or small to be represented). be represented).

Bit patterns can be interpreted in an endless Bit patterns can be interpreted in an endless number of ways, however important standards do number of ways, however important standards do existexist Two’s complementTwo’s complement IEEE 754 floating pointIEEE 754 floating point