39
Computer Systems Data Representation and Computer Arithmetic

Computer Systems Data Representation and Computer Arithmetic

Embed Size (px)

Citation preview

Page 1: Computer Systems Data Representation and Computer Arithmetic

Computer Systems

Data Representation andComputer Arithmetic

Page 2: Computer Systems Data Representation and Computer Arithmetic

Data Data RepresentationRepresentation

• Binary codes used to represent Binary codes used to represent numbersnumbers

• Conversion between number systemsConversion between number systems– Binary Binary decimal decimal– Hexadecimal Hexadecimal decimal decimal– Between arbitrary number systems Between arbitrary number systems

• Representation of positive/negative Representation of positive/negative numbersnumbers

• Floating point representationFloating point representation

Page 3: Computer Systems Data Representation and Computer Arithmetic

Bit, Byte and Bit, Byte and WordWord• BitBit is the smallest quantity that can be is the smallest quantity that can be

handled by computer: 1 or 0.handled by computer: 1 or 0.

• ByteByte is a group of 8 bits. is a group of 8 bits.

• WordWord is the basic unit of data that can is the basic unit of data that can be operated by computer: e.g., 16 bits.be operated by computer: e.g., 16 bits.

11001010 00001101Bit

Byte

Word•Some architectures have 8, 32, or 64-bit Some architectures have 8, 32, or 64-bit wordswords

Page 4: Computer Systems Data Representation and Computer Arithmetic

Content of WordContent of Word

• Bit PatternBit Pattern• May represent many thingsMay represent many things• Actual meaning of a particular Actual meaning of a particular bit pattern is given by the bit pattern is given by the programmerprogrammer• Computer itself cannot Computer itself cannot determine the meaning of the determine the meaning of the wordword• Classic question: Can you tell Classic question: Can you tell the meaning of a word picked the meaning of a word picked randomly from the memory?randomly from the memory?

Page 5: Computer Systems Data Representation and Computer Arithmetic

• Numeric QuantityNumeric Quantity

• Character:Character:A to Z, a to z, 0 to 9, *, -, +, etc.A to Z, a to z, 0 to 9, *, -, +, etc.

• ASCII Code:ASCII Code:- Representation of a character by 7 - Representation of a character by 7

bits.bits.

- The ASCII for W is 101 0111- The ASCII for W is 101 011122, or , or 57571616

• Instruction: (op-code)Instruction: (op-code)A single word defines an action A single word defines an action that is to be performed by CPUthat is to be performed by CPU

Page 6: Computer Systems Data Representation and Computer Arithmetic

ASCII CodeASCII Code 000 001 010 011 100 101 110 111 0000 NUL DLC SP 0 @ P . p 0001 SOH DC1 ! 1 A Q a q 0010 STX DC2 " 2 B R b r 0011 ETX DC3 # 3 C S c s 0100 EOT DC4 $ 4 D T d t 0101 ENQ NAK % 5 E U e u 0110 ACK SYN & 6 F V f v 0111 BEL ETB ' 7 G W g w 1000 BS CAN ( 8 H X h x 1001 HT EM ) 9 I Y i y 1010 LF SUB * : J Z j z 1011 VT ESC + ; K [ k { 1100 FF FS , < L \ l | 1101 CR GS - = M ] m } 1110 SO RS . > N ^ n ~ 1111 SI US / ? O _ o DEL

Page 7: Computer Systems Data Representation and Computer Arithmetic

Pixel (Picture Pixel (Picture Element)Element)• The smallest unit to construct a pictureThe smallest unit to construct a picture• 1bit/pixel for black-and-white pictures, 1bit/pixel for black-and-white pictures,

>1 bits/pixel for gray scale or color >1 bits/pixel for gray scale or color pictures, e.g. 24bits/pixel.pictures, e.g. 24bits/pixel.

• A letter is represented by a group of A letter is represented by a group of pixels on computer screen.pixels on computer screen.

Pixel (1)

Page 8: Computer Systems Data Representation and Computer Arithmetic

Positional NotationPositional Notation• Weight Weight is associated with the is associated with the

location within a number.location within a number.• The 9 in 95 has weight 10The 9 in 95 has weight 10

bmnnn aaaaaaaaN 210121 .

00

11 bababa n

nn

n m

m bababa

2

21

1

• A number N in A number N in base bbase b is represented is represented byby

Page 9: Computer Systems Data Representation and Computer Arithmetic

Example: Example:

210110 10510710510275.25

23452 2121202101.101101

2101 21202120 1025.45

Page 10: Computer Systems Data Representation and Computer Arithmetic

Number Number BasesBases

• Decimal (b=10): {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}Decimal (b=10): {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

• Binary (b=2): {0, 1}Binary (b=2): {0, 1}

• Octal (b=8): {0, 1, 2, 3, 4, 5, 6, 7}Octal (b=8): {0, 1, 2, 3, 4, 5, 6, 7}

• Hexadecimal (b=16):Hexadecimal (b=16):{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F}{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F}

Page 11: Computer Systems Data Representation and Computer Arithmetic

How many binary bits are How many binary bits are needed to represent an n-digits needed to represent an n-digits decimal?decimal?The value of an m-bit binary is up toThe value of an n-digit decimal is up

toso

12 m

110 n

nm 10log2log 1010 nm 2log10

nn

m 3.32log10

nmnm 10211012

nm 3.3OR

Page 12: Computer Systems Data Representation and Computer Arithmetic

How many bits are needed to How many bits are needed to represent an n-digits octal?represent an n-digits octal?

How many bits are needed to How many bits are needed to represent an n-digits hexadecimal?represent an n-digits hexadecimal?

QuestionsQuestions

Page 13: Computer Systems Data Representation and Computer Arithmetic

• Divide a decimal successively by 2, Divide a decimal successively by 2, record the remainder, until the result record the remainder, until the result of the division is 0.of the division is 0.

Conversion of IntegerConversion of Integer- Decimal to Binary- Decimal to Binary

67/2 = 3367/2 = 33 Rem.=1Rem.=133/2 = 1633/2 = 16 Rem.=1Rem.=116/2 = 816/2 = 8 Rem.=0Rem.=0 8/2 = 48/2 = 4 Rem.=0Rem.=0

4/24/2 = 2= 2 Rem.=0Rem.=02/22/2 = 1= 1 Rem.=0Rem.=01/21/2 = 0= 0 Rem.=1Rem.=1

210 0100001167

Page 14: Computer Systems Data Representation and Computer Arithmetic

Conversion of IntegerConversion of Integer- Decimal to - Decimal to

HexadecimalHexadecimal• Divide a decimal successively by Divide a decimal successively by 16, record the remainder, until the 16, record the remainder, until the result is 0result is 0

432/16 = 27432/16 = 27 Rem.=0Rem.=027/16 = 127/16 = 1 Rem.=BRem.=B1/16 = 01/16 = 0 Rem.=1Rem.=1

1610 001432 B

Page 15: Computer Systems Data Representation and Computer Arithmetic

Conversion of integerConversion of integer- Binary to Decimal- Binary to Decimal

34562 202120211010111

012 212121

Conversion of integerConversion of integer- Hexadecimal to - Hexadecimal to

DecimalDecimal012

16 161316111622 BD

10701

Page 16: Computer Systems Data Representation and Computer Arithmetic

Conversion of integerConversion of integer- Hexadecimal Binary- Hexadecimal Binary

0 1 0 1 0 1 1 1

5 7 1011

2 B 7

0010 0111

Conversion of integer:Conversion of integer:- Octal Binary- Octal Binary

0 1 0 1 0 1 1 1

2 71

2 5 7

010 111101

Page 17: Computer Systems Data Representation and Computer Arithmetic

Conversion of FractionConversion of Fraction- - Decimal to BinaryDecimal to Binary

• The fraction is repeatedly multiplied The fraction is repeatedly multiplied by 2, the integer part is stripped and by 2, the integer part is stripped and recorded.recorded.

0.68750.68751010 * 2 =1.375 * 2 =1.375 11

0.3750.3751010 * 2 = 0.75 * 2 = 0.75 00

0.750.751010 * 2 = 1.5 * 2 = 1.5 1 1

0.50.51010 * 2 = 1.0 * 2 = 1.0 11

0.00.01010 * 2 = 0.0 * 2 = 0.0 endend

0.687510 = 0.10112

Question: Question: Convert Convert 0.625? 0.625? 0.10.11010??

Page 18: Computer Systems Data Representation and Computer Arithmetic

Conversion of FractionConversion of Fraction- - Binary Binary

HexadecimalHexadecimal0.1010110012 = 0.1010 1100 10002

= 0.AC816

0.7BC16 = 0.0111 1011 11002

= 0.01111011112

Convert Convert 0.6D5?0.6D5?

Convert Convert 0.1100101010.11001010111

Page 19: Computer Systems Data Representation and Computer Arithmetic

Binary Coded Decimal Binary Coded Decimal (BCD)(BCD)• A decimal digit is coded into 4 bits A decimal digit is coded into 4 bits

1 byte can store 2 digits 1 byte can store 2 digits• Example: Example:

1942 is encoded as 1942 is encoded as

0001 1001 0100 0010 (2 bytes)0001 1001 0100 0010 (2 bytes)• DisadvantagesDisadvantages

• Complex arithmeticComplex arithmetic• Inefficient use of storageInefficient use of storage

• Advantage: Advantage: • Can represent “real” numberCan represent “real” number

DecimalDecimal BCDBCD

00 00000000

11 00010001

22 00100010

33 00110011

44 01000100

55 01010101

66 01100110

77 01110111

88 10001000

99 10011001

Page 20: Computer Systems Data Representation and Computer Arithmetic

Binary AdditionBinary Addition0 0 1 1 0 1 1 1

+ 0 1 0 1 0 1 1 0 1 1 1 1 1 Carry 1 0 0 0 1 1 0 1

Binary Binary Subtraction ?Subtraction ?• use 2’s complementary use 2’s complementary arithmetic (later … ) arithmetic (later … )

Page 21: Computer Systems Data Representation and Computer Arithmetic

Signed Integer Signed Integer RepresentationsRepresentations

• Sign-and-MagnitudeSign-and-Magnitude• 1’s Complement1’s Complement• 2’s Complement2’s Complement

Page 22: Computer Systems Data Representation and Computer Arithmetic

Sign-and-Magnitude Sign-and-Magnitude RepresentationRepresentation

• Use the most significant bit (MSB) to Use the most significant bit (MSB) to indicate the sign of the number.indicate the sign of the number.• The MSB is 0 for positive, 1 for The MSB is 0 for positive, 1 for negative.negative.• 8 bits represent -1278 bits represent -1271010 to 127 to 1271010

• Examples: 00101100 for +44Examples: 00101100 for +441010

10101100 for -4410101100 for -441010

Problem: 00000000 = +0, 10000000 = -Problem: 00000000 = +0, 10000000 = -00

Page 23: Computer Systems Data Representation and Computer Arithmetic

1’s Complement1’s Complement

• Just flip the bits!Just flip the bits!• The MSB is 0 for positive, 1 for The MSB is 0 for positive, 1 for negative.negative.• 8 bits represent -1278 bits represent -1271010 to 127 to 1271010

• Examples: 00101100 for +44Examples: 00101100 for +441010

11010011 for -4411010011 for -441010

Problem: 00000000 = +0, 11111111 = -Problem: 00000000 = +0, 11111111 = -00

Page 24: Computer Systems Data Representation and Computer Arithmetic

2’s Complement2’s Complement

• The 2’s complement for The 2’s complement for NN is is• Example: Using 5 bits (Example: Using 5 bits (n n = 5), if = 5), if N N = = 7, then 7, then

Nn 2

110010011125 12 -7 5

0 1 1 0 0+ 1 1 0 0 1 1) 0 0 1 0 1

• 2’s complement = 1’s complement 2’s complement = 1’s complement + 1+ 1 7 0 0 1 1 1

1’s complement 1 1 0 0 0 + 1

2’s complement 1 1 0 0 1 = -7

Page 25: Computer Systems Data Representation and Computer Arithmetic

2’s Complement 2’s Complement (con.)(con.)• To form the two’s complement of To form the two’s complement of a number, simply invert the bits (1 a number, simply invert the bits (1 to0, 0 to 1), and add 1 to0, 0 to 1), and add 1 1’s 1’s complement + 1complement + 1

13131010 = 0 0 0 0 1 1 0 1 = 0 0 0 0 1 1 0 1-13-131010 = 1 1 1 1 0 0 1 0 + 1 = 1 1 1 1 0 0 1 0 + 1 = 1 1 1 1 0 0 1 1= 1 1 1 1 0 0 1 1

2’s comp.2’s comp.

-34?-34?

Page 26: Computer Systems Data Representation and Computer Arithmetic

Properties of 2’s Properties of 2’s ComplementComplement• One unique 0One unique 0• MSB = 0, positive numberMSB = 0, positive number• MSB = 1, negative numberMSB = 1, negative number• The range isThe range is

• For 5 bits, the range is -16For 5 bits, the range is -161010 (10000) (10000) to +15to +151010 (01111) (01111)

• The 2’s complement of the The 2’s complement of the complement of X is X itself (Prove: complement of X is X itself (Prove:

) )

11)(n2to1)(n2

XXnn )2(2

Page 27: Computer Systems Data Representation and Computer Arithmetic

2’s Complementary 2’s Complementary ArithmeticArithmetic

• Subtraction is performed using additionA - B = A + (-B)

X = 910 = 010012, Y = 610 = 001102

-X = -910 = 101112, -Y = -610 = 110102

XX-Y-Y 331010

0 1 0 0 10 1 0 0 1+ 1 1 0 1 0+ 1 1 0 1 01) 0 0 0 1 11) 0 0 0 1 1

- X- X- Y- Y-15-151010

1 0 1 1 11 0 1 1 1+ 1 1 0 1 0+ 1 1 0 1 01) 1 0 0 0 11) 1 0 0 0 1

Page 28: Computer Systems Data Representation and Computer Arithmetic

Binary Adder/SubtractorBinary Adder/Subtractor

Page 29: Computer Systems Data Representation and Computer Arithmetic

Arithmetic OverflowArithmetic Overflow• The overflow happens whenThe overflow happens when

positive + positive positive + positive negative negativenegative + negative negative + negative positive positive

1212+13+13 2525

0 1 1 0 00 1 1 0 0+ 0 1 1 0 1+ 0 1 1 0 1 1 1 0 0 11 1 0 0 1

-12-12-13-13-25-25

1 0 1 0 01 0 1 0 0+ 1 0 0 1 1+ 1 0 0 1 11) 0 0 1 1 11) 0 0 1 1 1

1,

1,

1 ns

nb

na

111111 nnnnnn sbasbaV

• IfIf are MSBs, then the are MSBs, then the overflow bit v is expressed asoverflow bit v is expressed as

Page 30: Computer Systems Data Representation and Computer Arithmetic

Arithmetic OverflowArithmetic Overflow• In practice,In practice, 11 nnnn cCcCV

• Case I: A and B are +Case I: A and B are +

aann-1-1 = __ = __ bbnn-1-1 = __ = __

ccnn = __ = __ ccnn-1-1 = __ = __

VV = _____ = _____

• Case II: A and B are –Case II: A and B are –

aann-1-1 = __ = __ bbnn-1-1 = __ = __

ccnn = __ = __ ccnn-1-1 = __ = __

VV = _____ = _____

Most significant stage of full adderMost significant stage of full adder

Page 31: Computer Systems Data Representation and Computer Arithmetic

Fixed-point Fixed-point ArithmeticArithmetic 3.6253.6251010 0011.1010 0011.101022 0011101000111010+ 6.5+ 6.51010 0110.1000 0110.100022 + + 0110100001101000 10.12510.1251010 1010001010100010

The fraction point is addedThe fraction point is added10100010 -> 1010.001010100010 -> 1010.001022

2010What if we want to represent What if we want to represent ? ? We need at least m > 3.3*n = 3.3*20 We need at least m > 3.3*n = 3.3*20 = 66 bits != 66 bits !

Page 32: Computer Systems Data Representation and Computer Arithmetic

• Scientific NotationScientific Notation

a a is mantissa, is mantissa, rr is radix, is radix, ee is exponent is exponent

era

ea 2• is for binaryis for binary

Floating Point NumbersFloating Point Numbers

Page 33: Computer Systems Data Representation and Computer Arithmetic

IEEE Floating Point IEEE Floating Point FormatFormat

FX BEs .12)1(

SS: Sign bit, 1 bit: Sign bit, 1 bitEE: Exponent, 8 bits: Exponent, 8 bitsBB: Bias, 8 bits, 127: Bias, 8 bits, 1271010=0111 =0111 11111111FF: mantissa: mantissa

Page 34: Computer Systems Data Representation and Computer Arithmetic

• Normalization, e.g., Normalization, e.g., • Use sign and magnitude Use sign and magnitude representation for signed mantissa.representation for signed mantissa.• Use biased exponent, e.g. in excess Use biased exponent, e.g. in excess 127127

52010.100001010.0

•If we have 8 bits for exponent, to If we have 8 bits for exponent, to represent -5, we add 127represent -5, we add 1271010 (0111 (0111 1111) to -5, the result is 1221111) to -5, the result is 1221010=0111 =0111 1010101022..

IEEE Floating Point - IEEE Floating Point - ExampleExample

Page 35: Computer Systems Data Representation and Computer Arithmetic

IEEE Floating Point - IEEE Floating Point - ExampleExample

-2345.125 = -100100101001.001-2345.125 = -100100101001.00122

1) Normalization:1) Normalization:2) Negative mantissa, so 2) Negative mantissa, so SS = 1 = 13) Exponent:3) Exponent:

EE = 11 + 127 = 138 = 11 + 127 = 1381010 = 10001010 = 1000101022

4) Mantissa 4) Mantissa FF = 00100101001 0010…000 = 00100101001 0010…0001 10001010 00100101001001000…1 10001010 00100101001001000…

00

11210010010010100.1

Represent -2345.125 in 32 bits ?Represent -2345.125 in 32 bits ?

1.51.51010 in in IEEE IEEE format?format?

Page 36: Computer Systems Data Representation and Computer Arithmetic

• Zero: exponent and fraction all 0sZero: exponent and fraction all 0s• Denormalized: exponent all 0s, Denormalized: exponent all 0s,

fraction non-zerofraction non-zero

for single precision:for single precision:• Infinity: exponents all 1s, fraction all Infinity: exponents all 1s, fraction all 0s0s

sign bit determines +infinity or -sign bit determines +infinity or -infinityinfinity• Not a Number (NaN): exponents all Not a Number (NaN): exponents all 1s,1s,

fraction non-zerofraction non-zero

IEEE Floating Point Format – IEEE Floating Point Format – Special CasesSpecial Cases

FX s .02)1( 126

• A good reference is available atA good reference is available at

http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html html

Page 37: Computer Systems Data Representation and Computer Arithmetic

Bit Patterns and Logical Bit Patterns and Logical OperationsOperations

• ANDAND

• OROR

• NOTNOT

• Exclusive ORExclusive OR

They are bit-wise They are bit-wise operations.operations.

Page 38: Computer Systems Data Representation and Computer Arithmetic

AND AND OperationOperation• xx AND AND yy is true, if and only if both is true, if and only if both bits bits xx and and yy are true. are true.

A = 1 1 0 0 1 0 1 1A = 1 1 0 0 1 0 1 1B = 0 1 1 0 1 1 0 1B = 0 1 1 0 1 1 0 1

C=A AND B = 0 1 0 0 1 0 0 1C=A AND B = 0 1 0 0 1 0 0 1

OR OR OperationOperation• xx OR OR yy is true, if either bits is true, if either bits xx or or yy is true.is true.

A = 1 1 0 0 1 0 1 1A = 1 1 0 0 1 0 1 1B = 0 1 1 0 1 1 0 1B = 0 1 1 0 1 1 0 1

C=A OR B = 1 1 1 0 1 1 1 1C=A OR B = 1 1 1 0 1 1 1 1

Page 39: Computer Systems Data Representation and Computer Arithmetic

NOT OperationNOT Operation• A 1 becomes 0, and a 0 becomes A 1 becomes 0, and a 0 becomes 1.1.

A = 1 1 0 0 1 0 1 1A = 1 1 0 0 1 0 1 1 C = NOT A = 0 0 1 1 0 1 0 0C = NOT A = 0 0 1 1 0 1 0 0

EOR (Exclusive OR) EOR (Exclusive OR) OperationOperation• It is true, if and only if just one of It is true, if and only if just one of inputs is true.inputs is true.

A = 1 1 0 0 1 0 1 1A = 1 1 0 0 1 0 1 1 B = 0 1 1 0 1 1 0 1B = 0 1 1 0 1 1 0 1C = A EOR B = 1 0 1 0 0 1 1 0C = A EOR B = 1 0 1 0 0 1 1 0