Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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.

2 2 Road Map Boolean Algebra Logic Gates & Flip-flops Register-Transfer Design Finite-State Machines Binary Systems & Data Represent. Generalized FSM Sequential Design Techniques Logic Design Techniques Combinatorial Components Storage Components Processor Components 2 3 3 4 5 6 7 6 8 8 9

3 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 analog voltages 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 with just 1s and 0s. – Then we’ll introduce special operations for computing with 1s and 0s, by treating them as the logical values “true” and “false.” Volts 1.8 0 1 0

4 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 x 10 2 ) + (6 x 10 1 ) + (2 x 10 0 ) + (3 x 10 -1 ) + (7 x 10 -2 ) + (5 x 10 -3 ) = 162.375

5 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 x 2 3 ) + (1 x 2 2 ) + (0 x 2 1 ) + (1 x 2 0 ) + (0 x 2 -1 ) + (1 x 2 -2 ) = 8+ 4+ 0+ 1+ 0+ 0.25= 13.25

6 6 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 = 2 3, 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 7 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 = 2 4, 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 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.375 10 = 10100010.011 2 162 / 2= 81rem 0 81 / 2= 40rem 1 40 / 2= 20rem 0 20 / 2= 10rem 0 10 / 2= 5rem 0 5 / 2= 2rem 1 2 / 2= 1rem 0 1 / 2= 0rem 1 0.375 x 2 = 0.750 0.750 x 2 = 1.500 0.500 x 2 = 1.000

9 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= 16rem 2 16 / 10= 1rem 6 1 / 10= 0rem 1 0.375 x 10 = 3.750 0.750 x 10 = 7.500 0.500 x 10 = 5.000

10 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.35 16 = 2 6 1. 3 5 16 =001001100001.00110101 2 10110100.001011 2 =10110100.00101100 2 = B 4. 2 C 16

11 11 Practice Convert 32103.5 10 to hexadecimal. Convert CAFE 16 to decimal. Convert 703 8 to base-12.

12 12 Convert 32103.5 10 to hexadecimal. 1. 32103 / 16 = 2006rem 7 2. 2006 / 16 = 125rem 6 3. 125 / 16 = 7rem 13 4. 7 / 16 = 0rem 7 5..5 * 16 = 8.0 So 7D67.8 16.

13 13 Convert CAFE 16 to decimal. 1. C: 12 * 16 3 = 49152 2. A: 10 * 16 2 = 2560 3. F: 15 * 16 1 = 240 4. E: 14 * 16 - = 14 5. 49152+2560+240+14=51966

14 14 Convert 703 8 to base-12. 1. 7: 7 * 8 2 = 448 2. 0: 0 * 8 1 = 0 3. 3: 3 * 8 0 = 3 4. 448+0+3 = 451 5. 451 / 12 = 37rem 7 6. 37 / 12 = 3rem 1 7. 3 / 12 = 0rem 3 So 317 12.

15 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 16 Additional information Assistants: – Panca Novianto: panca.novianto@mhs.cs.ui.ac.idpanca.novianto@mhs.cs.ui.ac.id – Evan Jonathan Winata: evan.winata@mhs.cs.ui.ac.idevan.winata@mhs.cs.ui.ac.id

17 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 x 2 3 ) + (1 x 2 2 ) + (0 x 2 1 ) + (1 x 2 0 ) + (0 x 2 -1 ) + (1 x 2 -2 ) = 8+ 4+ 0+ 1+ 0+ 0.25= 13.25

18 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.375 10 = 10100010.011 2 162 / 2= 81rem 0 81 / 2= 40rem 1 40 / 2= 20rem 0 20 / 2= 10rem 0 10 / 2= 5rem 0 5 / 2= 2rem 1 2 / 2= 1rem 0 1 / 2= 0rem 1 0.375 x 2 = 0.750 0.750 x 2 = 1.500 0.500 x 2 = 1.000

19 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 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! 1110Carry in 1011Augend +1110Addend 11001Sum The initial carry in is implicitly 0 most significant bit, or MSB least significant bit, or LSB

21 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 22 Negative Numbers

23 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: 1101 2 = 13 10 (a 4-bit unsigned number) 01101= +13 10 (a positive number in 5-bit signed magnitude) 11101= -13 10 (a negative number in 5-bit signed magnitude) 0100 2 = 4 10 (a 4-bit unsigned number) 00100= +4 10 (a positive number in 5-bit signed magnitude) 10100= -4 10 (a negative number in 5-bit signed magnitude)

24 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. +379 + -647 -268 5136417-3792685136417-379268 because

25 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: 1101 2 = 13 10 (a 4-bit unsigned number) 01101= +13 10 (a positive number in 5-bit one’s complement) 10010= -13 10 (a negative number in 5-bit one’s complement) 0100 2 = 4 10 (a 4-bit unsigned number) 00100= +4 10 (a positive number in 5-bit one’s complement) 11011= -4 10 (a negative number in 5-bit one’s complement)

26 26 Why is it called “one’s complement?” Complementing a single bit is equivalent to subtracting it from 1. 0’ = 1, and 1 - 0 = 11’ = 0, and 1 - 1 = 0 Similarly, complementing each bit of an n-bit number is equivalent to subtracting that number from 2 n -1. For example, we can negate the 5-bit number 01101. – Here n=5, and 2 n -1 = 31 10 = 11111 2. – Subtracting 01101 from 11111 yields 10010: 11111-011011001011111-0110110010

27 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) 10010 0010 + 1 0011(+3) +0010+(+2) 00101 0101 + 0 0101(+5)

28 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: 1101 2 = 13 10 (a 4-bit unsigned number) 01101= +13 10 (a positive number in 5-bit two’s complement) 10010= -13 10 (a negative number in 5-bit ones’ complement) 10011= -13 10 (a negative number in 5-bit two’s complement) 0100 2 = 4 10 (a 4-bit unsigned number) 00100= +4 10 (a positive number in 5-bit two’s complement) 11011= -4 10 (a negative number in 5-bit ones’ complement) 11100= -4 10 (a negative number in 5-bit two’s complement)

29 29 Two other equivalent ways to negate two’s complement numbers: – You can subtract an n-bit two’s complement number from 2 n. – You can complement all of the bits to the left of the rightmost 1. 01101= +13 10 (a positive number in two’s complement) 10011= -13 10 (a negative number in two’s complement) 00100= +4 10 (a positive number in two’s complement) 11100= -4 10 (a negative number in two’s complement) More about two’s complement 100000 -01101(+13 10 ) 10011(-13 10 ) 100000 -00100(+4 10 ) 11100(-4 10 )

30 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.

31 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 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 +21 10 in decimal. So 110101 must represent -21 10. (b) ones’ complement Negating 110101 in ones’ complement yields 001010 = +10 10, so the original number must have been -10 10. (c) two’s complement Negating 110101 in two’s complement gives 001011 = 11 10, which means 110101 = -11 10. The most important point here is that a binary number has different meanings depending on which representation is assumed.

33 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 0  0 1 0 0(+4)+1 1 0 0(-4) 1 1 1 0 (-2) 1 1 1 0(-2) 1 1 1 0  1 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 34 Examples: 7 + 3 = 10 but... - 4 – 5 = - 9 but... 2’s ComplementBinaryDecimal 00000 10001 20010 30011 0000 1111 1110 1101 Decimal 0 -2 -3 40100 50101 60110 70111 1100 1011 1010 1001 -4 -5 -6 -7 1000-8 0111 0011+ 1010 1 1100 1011+ 0111 110 7 3 1 – 6 – 4 – 5 7 Overflow

35 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. 1101Multiplicand x0110Multiplier 0000Partial products 1101 +0000 1001110Product

36 36 Shifting the multiplicand 1101Multiplicand x0110Multiplier 0000first partial products +0000shifted zeros 0000second partial products +1101shifted multiplicand 11010third partial products +1101shifted multiplicand 1001110fourth partial products +0000shifted zeros 1001110Product

37 37 Binary Division

38 38 Floating-Point Numbers

39 39 6.02 x 10 23 radix (base) decimal pointmantissa 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 40 1.0 two 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 41 Normal format: +1.xxxxxxxxxx two *2 yyyy two Multiple of Word Size (32 bits) 31 0 SExponent 189 Significand 1 bit8 bits23 bits S represents Sign Exponent represents y’s Significand represents x’s Represent numbers as small as 2.0 x 10 -38 to as large as 2.0 x 10 38 Floating point representation

42 42 BCD

43 43 Character Codes

44 44 Codes for Error Detection & Correction Please read yourself.

45 45 Hamming Codes Please read yourself.


Download ppt "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."

Similar presentations


Ads by Google