IKI10201 02-Data Types & Representations Bobby Nazief Semester-I 2005 - 2006 The materials on these...

45
IKI10201 02-Data Types & Representations Bobby Nazief Semester-I 2005 - 2006 The materials on these slides are adopted from those in CS231’s Lecture Notes at UIUC, which is derived from Howard Huang’s work and developed by Jeff Carlyle.

Transcript of IKI10201 02-Data Types & Representations Bobby Nazief Semester-I 2005 - 2006 The materials on these...

IKI1020102-Data Types & Representations

Bobby NaziefSemester-I 2005 -

2006

The materials on these slides are adopted from those in CS231’s Lecture Notes at UIUC, which is derived from Howard Huang’s work and developed by Jeff Carlyle.

2

Road Map

Boolean Algebra

Logic Gates &Flip-flops

Register-TransferDesign

Finite-StateMachines

Binary Systems& Data Represent.

Generalized FSM

Sequential DesignTechniques

Logic DesignTechniques

CombinatorialComponents

StorageComponents

ProcessorComponents

2

3

3

4

5

6

7

6

8 8

9

3

Number Systems

• To get started, we’ll discuss one of the fundamental concepts underlying digital computer design:

Deep down inside, computers work with just 1s and 0s.

• Computers use voltages to represent information. In modern CPUs the voltage is usually limited to 1.6-1.8V to minimize power consumption.

• It’s convenient for us to translate these analogvoltages into the discrete, or digital, values 1 and 0.

• But how can two lousy digits be useful for anything?

– First, we’ll see how to represent numbers withjust 1s and 0s.

– Then we’ll introduce special operationsfor computing with 1s and 0s, by treating them asthe logical values “true” and “false.”

Volts1.8

0

1

0

4

Positional number systems: decimal

• Numbers consist of a bunch of digits, each with a weight:

• The weights are all powers of the base, which is 10. We can rewrite the weights like this:

• To find the decimal value of a number, multiply each digit by its weight and sum the products.

1 6 2 . 3 7 5 Digits100 10 1 1/ 10 1/ 100 1/ 1000 Weights

1 6 2 . 3 7 5 Digits102 101 100 10-1 10-2 10-3 Weights

(1 x 102) + (6 x 101) + (2 x 100) + (3 x 10-1) + (7 x 10-2) + (5 x 10-3) = 162.375

5

Positional number systems: binary

• We can use the same trick for binary, or base 2, numbers. The only difference is that the weights are powers of 2.

• For example, here is 1101.01 in binary:

• The decimal value is:

1 1 0 1 . 0 1 Binary digits, or bits 23 22 21 20 2-1 2-2 Weights (in base 2)

(1 x 23) + (1 x 22) + (0 x 21) + (1 x 20) + (0 x 2-1) + (1 x 2-2) = 8 + 4 + 0 + 1 + 0 + 0.25 = 13.25 Powers of 2:

20 = 1 24 = 16 28 = 25621 = 2 25 = 32 29 = 51222 = 4 26 = 64 210 = 102423 = 8 27 = 128

6

Decimal Binary Octal 0 000 0 1 001 1 2 010 2 3 011 3 4 100 4 5 101 5 6 110 6 7 111 7

Decimal Binary Octal 0 000 0 1 001 1 2 010 2 3 011 3 4 100 4 5 101 5 6 110 6 7 111 7

Base 8 - Octal

• The octal system uses 8 digits:

0 1 2 3 4 5 6 7

• Earlier in this history of computing, octal was used as a shorthand for binary numbers. (Now hexadecimal is more common.)

• Since 8 = 23, one octal digit is equivalent to 3 binary digits.

– Numbers like 67 are easier to work with than 110111.

• Still shows up in some places. For instance, file access permissions in Unix file systems.

7

Decimal Binary Hex0 0000 01 0001 12 0010 23 0011 34 0100 45 0101 56 0110 67 0111 78 1000 89 1001 910 1010 A11 1011 B12 1100 C13 1101 D14 1110 E15 1111 F

Decimal Binary Hex0 0000 01 0001 12 0010 23 0011 34 0100 45 0101 56 0110 67 0111 78 1000 89 1001 910 1010 A11 1011 B12 1100 C13 1101 D14 1110 E15 1111 F

Base 16 is useful too

• The hexadecimal system uses 16 digits:

0 1 2 3 4 5 6 7 8 9 A B C D E F

• For our purposes, base 16 is most useful as a “shorthand” notation for binary numbers.

– Since 16 = 24, one hexadecimal digit is equivalent to 4 binary digits.

– It’s often easier to work with a number like B4 instead of 10110100.

• Hex is frequently used to specify things like IPv6 addresses and 24-bit colors.

8

Converting decimal to binary

• To convert a decimal integer into binary, keep dividing by 2 until the quotient is 0. Collect the remainders in reverse order.

• To convert a fraction, keep multiplying the fractional part by 2 until it becomes 0. Collect the integer parts in forward order.

• Example: 162.375:

• So, 162.37510 = 10100010.0112

162 / 2 = 81 rem 0 81 / 2 = 40 rem 1 40 / 2 = 20 rem 0 20 / 2 = 10 rem 0 10 / 2 = 5 rem 0 5 / 2 = 2 rem 1 2 / 2 = 1 rem 0 1 / 2 = 0 rem 1

0.375 x 2 = 0.7500.750 x 2 = 1.5000.500 x 2 = 1.000

9

Why does this work?

• This works for converting from decimal to any base

• Why? Think about converting 162.375 from decimal to decimal.

• Each division strips off the rightmost digit (the remainder). The quotient represents the remaining digits in the number.

• Similarly, to convert fractions, each multiplication strips off the leftmost digit (the integer part). The fraction represents the remaining digits.

162 / 10 = 16 rem 2 16 / 10 = 1 rem 6 1 / 10 = 0 rem 1

0.375 x 10 = 3.7500.750 x 10 = 7.5000.500 x 10 = 5.000

10

Binary and hexadecimal conversions

• Converting from hexadecimal to binary is easy: just replace each hex digit with its equivalent 4-bit binary sequence.

• To convert from binary to hex, make groups of 4 bits, starting from the binary point. Add 0s to the ends of the number if needed. Then, just convert each bit group to its corresponding hex digit.

261.3516 = 2 6 1 . 3 516

= 0010 0110 0001 . 0011 01012

10110100.0010112 = 1011 0100 .0010 11002

= B 4 . 2 C16

1111F1011B0111700113

1110E1010A0110600102

1101D100190101500011

1100C100080100400000

BinaryHexBinaryHexBinaryHexBinaryHex

11

Practice

• Convert 32103.510 to hexadecimal.

• Convert CAFE16 to decimal.

• Convert 7038 to base-12.

12

Convert 32103.510 to hexadecimal.

1. 32103 / 16 = 2006 rem 72. 2006 / 16 = 125 rem 63. 125 / 16 = 7 rem 134. 7 / 16 = 0 rem 75. .5 * 16 = 8.0

• So 7D67.816.

13

Convert CAFE16 to decimal.

1. C: 12 * 163 = 491522. A: 10 * 162 = 25603. F: 15 * 161 = 2404. E: 14 * 16- = 14

5. 49152+2560+240+14=51966

14

Convert 7038 to base-12.

1. 7: 7 * 82 = 4482. 0: 0 * 81 = 03. 3: 3 * 80 = 3

4. 448+0+3 = 451

5. 451 / 12 = 37 rem 76. 37 / 12 = 3 rem 17. 3 / 12 = 0 rem 3

• So 31712.

15

Number Systems Summary

• Computers are binary devices.

– We’re forced to think in terms of base 2.

– Today we learned how to convert numbers between binary, decimal and hexadecimal.

• We’ve already seen some of the recurring themes of architecture:

– We use 0 and 1 as abstractions for analog voltages.

– We showed how to represent numbers using just these two signals.

16

Additional information

• Assistants:

– Panca Novianto: [email protected]

– Evan Jonathan Winata: [email protected]

17

Review - Positional number systems: binary

• We can use the same trick for binary, or base 2, numbers. The only difference is that the weights are powers of 2.

• For example, here is 1101.01 in binary:

• The decimal value is:

1 1 0 1 . 0 1 Binary digits, or bits 23 22 21 20 2-1 2-2 Weights (in base 2)

(1 x 23) + (1 x 22) + (0 x 21) + (1 x 20) + (0 x 2-1) + (1 x 2-2) = 8 + 4 + 0 + 1 + 0 + 0.25 = 13.25 Powers of 2:

20 = 1 24 = 16 28 = 25621 = 2 25 = 32 29 = 51222 = 4 26 = 64 210 = 102423 = 8 27 = 128

18

Review - Converting decimal to binary

• To convert a decimal integer into binary, keep dividing by 2 until the quotient is 0. Collect the remainders in reverse order.

• To convert a fraction, keep multiplying the fractional part by 2 until it becomes 0. Collect the integer parts in forward order.

• Example: 162.375:

• So, 162.37510 = 10100010.0112

162 / 2 = 81 rem 0 81 / 2 = 40 rem 1 40 / 2 = 20 rem 0 20 / 2 = 10 rem 0 10 / 2 = 5 rem 0 5 / 2 = 2 rem 1 2 / 2 = 1 rem 0 1 / 2 = 0 rem 1

0.375 x 2 = 0.7500.750 x 2 = 1.5000.500 x 2 = 1.000

19

Addition and Subtraction of Binary Numbers

• Arithmetic is the most basic thing you can do with a computer, but it’s not as easy as you might expect!

• These next few lectures focus on addition and subtraction.

• Computers were designed to compute, so arithmetic is at the heart of a CPU.

20

Binary addition by hand

• You can add two binary numbers one column at a time starting from the right, just as you add two decimal numbers.

• But remember that it’s binary. For example, 1 + 1 = 10 and you have to carry!

1 1 1 0 Carry in1 0 1 1 Augend

+ 1 1 1 0 Addend1 1 0 0 1 Sum

The initial carryin is implicitly 0

most significantbit, or MSB

least significantbit, or LSB

21

Subtraction

• Computers do subtraction by adding the minuend with the negative representation of the subtrahend:

– The main problem is representing negative numbers in binary. We introduce three methods, and show why one of them is the best.

– With negative numbers, we’ll be able to do subtraction using the adders we made last time, because A - B = A + (-B).

22

Negative Numbers

23

Signed magnitude representation

• Humans use a signed-magnitude system: we add + or - in front of a magnitude to indicate the sign.

• We could do this in binary as well, by adding an extra sign bit to the front of our numbers. By convention:

– A 0 sign bit represents a positive number.

– A 1 sign bit represents a negative number.

• Examples:

11012 = 1310 (a 4-bit unsigned number)

01101 = +1310 (a positive number in 5-bit signed magnitude)11101 = -1310 (a negative number in 5-bit signed magnitude)01002 = 410 (a 4-bit unsigned number)

00100 = +410 (a positive number in 5-bit signed magnitude)10100 = -410 (a negative number in 5-bit signed magnitude)

24

Signed magnitude operations

• Negating a signed-magnitude number is trivial: just change the sign bit from 0 to 1, or vice versa.

• Adding numbers is difficult, though. Signed magnitude is basically what people use, so think about the grade-school approach to addition. It’s based on comparing the signs of the augend and addend:

– If they have the same sign, add the magnitudes and keep that sign.

– If they have different signs, then subtract the smaller magnitude from the larger one. The sign of the number with the larger magnitude is the sign of the result.

• This method of subtraction would lead to a rather complex circuit.

+ 3 7 9+ - 6 4 7 -2 6 8

5 136 4 17

- 3 7 92 6 8

because

25

One’s complement representation

• A different approach, one’s complement, negates numbers by complementing each bit of the number.

• We keep the sign bits: 0 for positive numbers, and 1 for negative. The sign bit is complemented along with the rest of the bits.

• Examples:

11012 = 1310 (a 4-bit unsigned number)

01101 = +1310 (a positive number in 5-bit one’s complement)10010 = -1310 (a negative number in 5-bit one’s complement)01002 = 410 (a 4-bit unsigned number)

00100 = +410 (a positive number in 5-bit one’s complement)11011 = -410 (a negative number in 5-bit one’s complement)

26

Why is it called “one’s complement?”

• Complementing a single bit is equivalent to subtracting it from 1.

0’ = 1, and 1 - 0 = 1 1’ = 0, and 1 - 1 = 0

• Similarly, complementing each bit of an n-bit number is equivalent to subtracting that number from 2n-1.

• For example, we can negate the 5-bit number 01101.

– Here n=5, and 2n-1 = 3110 = 111112.

– Subtracting 01101 from 11111 yields 10010:

1 1 1 1 1- 0 1 1 0 1

1 0 0 1 0

27

One’s complement addition

• To add one’s complement numbers:

– First do unsigned addition on the numbers, including the sign bits.

– Then take the carry out and add it to the sum.

• Two examples:

• This is simpler and more uniform than signed magnitude addition.

0111 (+7)+ 1011 + (-4)

1 0010

0010+ 1

0011 (+3)

0011 (+3)+ 0010 + (+2)

0 0101

0101+ 0

0101 (+5)

28

Two’s complement

• Our final idea is two’s complement. To negate a number, complement each bit (just as for ones’ complement) and then add 1.

• Examples:

11012 = 1310 (a 4-bit unsigned number)

01101 = +1310 (a positive number in 5-bit two’s complement)10010 = -1310 (a negative number in 5-bit ones’ complement)10011 = -1310 (a negative number in 5-bit two’s complement)

01002 = 410 (a 4-bit unsigned number)

00100 = +410 (a positive number in 5-bit two’s complement)11011 = -410 (a negative number in 5-bit ones’ complement)11100 = -410 (a negative number in 5-bit two’s complement)

29

• Two other equivalent ways to negate two’s complement numbers:

– You can subtract an n-bit two’s complement number from 2n.

– You can complement all of the bits to the left of the rightmost 1.

01101 = +1310 (a positive number in two’s complement)

10011 = -1310 (a negative number in two’s complement)

00100 = +410 (a positive number in two’s complement)

11100 = -410 (a negative number in two’s complement)

More about two’s complement

100000- 01101 (+1310)

10011 (-1310)

100000- 00100 (+410)

11100 (-410)

30

Comparing the signed number systems

• Here are all the 4-bit numbers in the different systems.

• Positive numbers are the same in all three representations.

• Signed magnitude and one’s complement have two ways of representing 0. This makes things more complicated.

• Two’s complement has asymmetric ranges; there is one more negative number than positive number. Here, you can represent -8 but not +8.

• However, two’s complement is preferred because it has only one 0, and its addition algorithm is the simplest.

Decimal S.M. 1’s comp. 2’s comp.

7 0111 0111 0111 6 0110 0110 0110 5 0101 0101 0101 4 0100 0100 0100 3 0011 0011 0011 2 0010 0010 0010 1 0001 0001 0001 0 0000 0000 0000 -0 1000 1111 — -1 1001 1110 1111 -2 1010 1101 1110 -3 1011 1100 1101 -4 1100 1011 1100 -5 1101 1010 1011 -6 1110 1001 1010 -7 1111 1000 1001 -8 — — 1000

31

Converting signed numbers to decimal

• Convert 110101 to decimal, assuming this is a number in:

(a) signed magnitude format

(b) ones’ complement

(c) two’s complement

32

Example solution

• Convert 110101 to decimal, assuming this is a number in:

Since the sign bit is 1, this is a negative number. The easiest way to find the magnitude is to convert it to a positive number.

(a) signed magnitude format

Negating the original number, 110101, gives 010101, which is +2110 in decimal. So 110101 must represent -2110.

(b) ones’ complement

Negating 110101 in ones’ complement yields 001010 = +1010, so the original number must have been -1010.

(c) two’s complement

Negating 110101 in two’s complement gives 001011 = 1110, which means 110101 = -1110.

• The most important point here is that a binary number has different meanings depending on which representation is assumed.

33

• Addition:

– Just add the two numbers

– Ignore the Carry-out from MSB

– Result will be correct, provided there’s no overflow

0 1 0 1 (+5)+0 0 1 0 (+2) 0 1 1 1 (+7)

0 1 0 1 (+5)+1 0 1 0 (-6) 1 1 1 1 (-1)

1 0 1 1 (-5)+1 1 1 0 (-2)11 0 0 1 (-7)

0 1 1 1 (+7)+1 1 0 1 (-3)10 1 0 0 (+4)

0 0 1 0 (+2) 0 0 1 00 1 0 0 (+4) +1 1 0 0 (-4)

1 1 1 0 (-2)

1 1 1 0 (-2) 1 1 1 01 0 1 1 (-5) +0 1 0 1 (+5)

10 0 1 1 (+3)

• Subtraction:

– Form 2’s complement of the subtrahend

– Add the two numbers as in Addition

Addition & Subtraction of 2’s complement numbers

34

• Examples: 7 + 3 = 10 but ... - 4 – 5 = - 9 but ...

2’s ComplementBinaryDecimal

0 0000

1 0001

2 0010

3 0011

0000

1111

1110

1101

Decimal

0

-1

-2

-3

4 0100

5 0101

6 0110

7 0111

1100

1011

1010

1001

-4

-5

-6

-7

1000-8

0 1 1 1

0 0 1 1+

1 0 1 0

1

1 1 0 0

1 0 1 1+

0 1 1 1

110

7

3

1

– 6

– 4

– 5

7

Overflow

35

Binary Multiplication

• Since we always multiply by either 0 or 1, the partial products are always either 0000 or the multiplicand (1101 in this example).

• There are four partial products which are added to form the result.

1 1 0 1 Multiplicandx 0 1 1 0 Multiplier

0 0 0 0 Partial products1 1 0 1

1 1 0 1+ 0 0 0 0

1 0 0 1 1 1 0 Product

36

Shifting the multiplicand

1 1 0 1 Multiplicandx 0 1 1 0 Multiplier

0 0 0 0 first partial products+ 0 0 0 0 shifted zeros

0 0 0 0 second partial products+ 1 1 0 1 shifted multiplicand

1 1 0 1 0 third partial products+ 1 1 0 1 shifted multiplicand

1 0 0 1 1 1 0 fourth partial products + 0 0 0 0 shifted zeros

1 0 0 1 1 1 0 Product

37

Binary Division

38

Floating-Point Numbers

39

6.02 x 1023

radix (base)decimal point

mantissa exponent

• Normalized form: no leadings 0s (exactly one digit to left of decimal point)

• Alternatives to representing 1/1,000,000,000

– Normalized: 1.0 x 10-9

– Not normalized: 0.1 x 10-8,10.0 x 10-10

Scientific notation review

40

1.0two x 2-1

radix (base)“binary point”

Mantissa exponent

•Computer arithmetic that supports it called floating point, because it represents numbers where binary point is not fixed, as it is for integers

– Declare such variable in C as float

Scientific notation for binary numbers

41

•Normal format: +1.xxxxxxxxxxtwo*2yyyytwo

•Multiple of Word Size (32 bits)

310S Exponent1 8 9

Significand

1 bit 8 bits 23 bits

•S represents SignExponent represents y’sSignificand represents x’s

•Represent numbers as small as 2.0 x 10-38 to as large as 2.0 x 1038

Floating point representation

42

BCD

43

Character Codes

44

Codes for Error Detection & Correction

• Please read yourself.

45

Hamming Codes

• Please read yourself.