1 / 54

Data Representation

Data Representation. Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University. Course Contents. Introduction to computer systems: B&O 1 Data representations: B&O 2.1 – 2.4 Introduction to C programming: K&R 1 – 4

marnin
Télécharger la présentation

Data Representation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Representation Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University

  2. Course Contents • Introduction to computer systems: B&O 1 • Data representations: B&O 2.1 – 2.4 • Introduction to C programming: K&R 1 – 4 • C: advanced topics: K&R 5.1 – 5.10, 6 – 7 • Introduction to IA32 (Intel Architecture 32): B&O 3.1 – 3.8, 3.13 • Compiling, linking, loading, and executing: B&O 7 (except 7.12) • Dynamic memory management – Heap: B&O 9.9.1 – 9.9.2, 9.9.4 – 9.9.5, 9.11 • Code optimization: B&O 5.1 – 5.6, 5.13 • Memory hierarchy, locality, caching: B&O 5.12, 6.1 – 6.3, 6.4.1 – 6.4.2, 6.5, 6.6.2 – 6.6.3, 6.7 • Virtual memory (if time permits): B&O 9.4 – 9.5 Data Representation

  3. Unit Learning Objectives • Convert a decimal number to binary, Octal, Hexadecimal. • Convert a hexadecimal number to binary number, or Octal and vice versa • Compute binary addition. • Compute binary subtraction using 2’s complement. • Determine the 2’s complement representation of a signed integer. • Understand the overflow of unsigned integers and signed integers. • Understand the 2’s complement representation for negative integers. • Subtract a binary number by using the 2’s complement addition. • Multiply two binary numbers. • Distinguish little endian byte order and big endian byte order • Use of left shift and right shift. • Binary division Data Representation

  4. 1. Information Storage • memory, address • Memory space is partitioned into more manageable units to store the different program objects, i.e., instructions, program data, and control information. • The actual machine level program simply treats each program object as a block of bytes, and the program itself as a sequence of bytes. • Example • int number = 28; • 4 bytes, i.e., 32 bits, will be allocated to the variable number. • The decimal number 28 will be stored in the 32 bits? How? • Binary number, not the decimal, 00000000 00000000 00000000 00011100 will be stored in the 32 bits. • Do programmers have to convert 28 to it’s binary number? Data Representation

  5. 2 1 0 -1 -2 100 10 1 0.1 0.01 500 10 2 0.7 0.04 The Decimal System • Base (also called radix) = 10 • 10 digits { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } • Digit Position • Integer & fraction • Digit Weight • Weight = (Base) Position • Magnitude • Sum of “Digit x Weight” • Formal Notation 5 1 2 7 4 d2*B2+d1*B1+d0*B0+d-1*B-1+d-2*B-2 (512.74)10 Number Systems

  6. The Binary System • In computer systems, the most basic memory unit is a bit that contains 0 and 1. • The data unit of 8 bits is referred as a byte that is the basic memory unit used in main memories and hard disks. • All data are represented by using binary numbers. Data types such as text, voice, image and video have no meaning in the data representation. • 8 bits are usually used to express English alphabets. • A collection of nbits has 2npossible states. Is it true? • E.g., • How many different numbers can you express using 2 bits? • How many different numbers can you express using 4 bits? • How many different numbers can you express using 8 bits? • How many different numbers can you express using 32 bits? Number Systems

  7. 4 2 1 1/2 1/4 2 1 0 -1 -2 Binary Number System • Base = 2 • 2 digits { 0, 1 }, called binary digits or “bits” • Weights • Weight = (Base) Position • Magnitude • Sum of “Bit x Weight” • Formal Notation • Groups of bits 4 bits = Nibble 8 bits = Byte Kilobyte (KB) = 1,024 Bytes Megabyte (MB) = 1,024 Kilobytes Gigabyte (GB) =1,024 Megabytes Terabyte (TB) = 1,024 Gigabytes Petabyte (PB) = 1,024 Terabytes Exabyte (EB) = 1,024 Petabytes 1 0 1 0 1 1 *22+0 *21+1 *20+0 *2-1+1 *2-2 =(5.25)10 (101.01)2 1 0 1 1 1 1 0 0 0 1 0 1 Number Systems

  8. 64 8 1 1/8 1/64 2 1 0 -1 -2 Octal Number System • Base = 8 • 8 digits { 0, 1, 2, 3, 4, 5, 6, 7 } • Weights • Weight = (Base) Position • Magnitude • Sum of “Digit x Weight” • Formal Notation 5 1 2 7 4 5 *82+1 *81+2 *80+7 *8-1+4 *8-2 =(330.9375)10 (512.74)8

  9. 256 16 1 1/16 1/256 2 1 0 -1 -2 Hexadecimal Number System • Base = 16 • 16 digits { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F } • Weights • Weight = (Base) Position • Magnitude • Sum of “Digit x Weight” • Formal Notation 1 E 5 7 A 1 *162+14 *161+5 *160+7 *16-1+10 *16-2 =(485.4765625)10 (1E5.7A)16

  10. The Power of 2 Kilo Mega Giga Tera

  11. Conversion Chart • Uses two digits 0 and 1. How to expand binary numbers? • 02 = 0×20 = 010 • 12 = 1×20 = 110 • 102 = 1×21 + 0×20 = 210 • 112 = 1×21 + 1×20 = 310 • 1002 = 1×22 + 0×21 + 0×20 = 410 • 1012 = 1×22 + 0×21 + 1×20 = 510 • 1102 = 1×22 + 1×21 + 0×20 = 610 • 1112 = 1×22 + 1×21 + 1×20 = 710 • 10002 = 1×23 + 0×22 + 0×21 + 0×20 = 810 • 10012 = 1×23 + 0×22 + 0×21 + 1×20 = 910 • ... Number Systems

  12. Powers of 2 • 12 = 1×20 = 110 • 102 = 1×21 + 0×20 = 210 • 1002 = 1×22 + 0×21 + 0×20 = 410 • 10002 = 1×23 + 0×22 + 0×21 + 0×20 = 810 • 1 00002 = 1610 • 10 00002 = 3210 • 100 00002 = 6410 Number Systems

  13. Number Base Conversions Evaluate Magnitude Octal (Base 8) Evaluate Magnitude Decimal (Base 10) Binary (Base 2) Hexadecimal (Base 16) Evaluate Magnitude

  14. Decimal to Binary Conversion • Divide the number by the ‘Base’ (=2) • Take the remainder (either 0 or 1) as a coefficient • Take the quotient and repeat the division Example: (13)10 Coefficient Quotient Remainder 13 / 2 =6 1a0 =1 6 / 2 =3 0a1 =0 3 / 2 =1 1a2 =1 1 / 2 =0 1a3 =1 Answer: (13)10= (a3 a2 a1 a0)2= (1101)2 MSB LSB

  15. Decimal to Binary Conversion • Multiply the number by the ‘Base’ (=2) • Take the integer (either 0 or 1) as a coefficient • Take the resultant fraction and repeat the division Example: (0.625)10 Coefficient Integer Fraction a-1 =1 0.625 * 2 =1 . 25 0.25 * 2 =0 . 5a-2 =0 0.5 * 2 =1 . 0a-3 =1 Answer: (0.625)10= (0.a-1 a-2 a-3)2= (0.101)2 MSB LSB

  16. Decimal to Octal Conversion Example: (175)10 Coefficient Quotient Remainder 175 / 8 =21 7a0 =7 21 / 8 =2 5a1 =5 2 / 8 =0 2a2 =2 Answer: (175)10= (a2 a1 a0)8= (257)8 Example: (0.3125)10 Coefficient Integer Fraction a-1 =2 0.3125 * 8 =2 . 5 0.5 * 8 =4 . 0a-2 =4 Answer: (0.3125)10= (0.a-1 a-2 a-3)8= (0.24)8

  17. Binary − Octal Conversion • 8 = 23 • Each group of 3 bits represents an octal digit Example: Assume Zeros ( 1 0 1 1 0 . 0 1 )2 ( 2 6 . 2 )8 Works both ways (Binary to Octal & Octal to Binary)

  18. Binary − Hexadecimal Conversion • 16 = 24 • Each group of 4 bits represents a hexadecimal digit Example: Assume Zeros ( 1 0 1 1 0 . 0 1 )2 ( 1 6 . 4 )16 Works both ways (Binary to Hex & Hex to Binary)

  19. Octal − Hexadecimal Conversion • Convert to Binary as an intermediate step Example: ( 2 6 . 2 )8 Assume Zeros Assume Zeros ( 01 0 1 1 0 . 0 1 0 )2 ( 1 6 . 4 )16 Works both ways (Octal to Hex & Hex to Octal)

  20. Decimal, Binary, Octal and Hexadecimal

  21. Decimal Addition • Decimal Addition 1 1 Carry 5 5 + 5 5 1 1 0 = Ten ≥Base  Subtract a Base

  22. Binary Addition • Column Addition 1 1 1 1 1 1 = 61 = 23 1 1 1 1 0 1 + 1 0 1 1 1 = 84 1 0 1 0 1 0 0 ≥ (2)10

  23. Binary Subtraction • Borrow a “Base” when needed 1 2 = (10)2 2 0 2 0 0 2 1 = 77 = 23 0 0 1 1 0 1 − 1 0 1 1 1 0 1 1 0 1 1 0 = 54

  24. Binary Multiplication • Bit by bit 1 0 1 1 1 x 1 0 1 0 0 0 0 0 0 1 0 1 1 1 0 0 0 0 0 1 0 1 1 1 1 1 1 0 0 1 1 0

  25. Hexadecimal Number System • 010 = 00002 = 016 = 0x0 • 110 = 00012 = 116 = 0x1 • 210 = 00102 = 216 = 0x2 • 310 = 00112 = 316 = 0x3 • 410 = 01002 = 416 = 0x4 • 510 = 01012 = 516 = 0x5 • 610 = 01102 = 616 = 0x6 • 710 = 01112 = 716 = 0x7 • 810 = 10002 = 816 = 0x8 • 910 = 10012 = 916 = 0x9 • 1010 = 10102 = A16 = 0xA • 1110 = 10112 = B16 = 0xB • 1210 = 11002 = C16 = 0xC • 1310 = 11012 = D16 = 0xD • 1410 = 11102 = E16 = 0xE • 1510 = 11112 = F16 = 0xF • 14816= ???1014816= ???2 • 23c9d6ef = ???1023c9d6ef = ???2 4 bitscan be used for a hexadecimal number, 0, ..., F. Please memorize it! Number Systems

  26. Binary to/from Hexadecimal conversion • 4DA916= ???2 • 1001101101010012 = ???16 = 100 1101 1010 1001 = 4DA9 • 10 11102 = 0x??? = ???10 • 0100 1110 1011 1001 01002 = 0x??? = ???10 Number Systems

  27. Words • A word size – the nominal size of integer and pointer data. A pointer variable contains an address in the virtual address space. We will discuss pointer variable in the next unit. • The word size determines the maximum size of the virtual address space. • 32bit operating systems? • 64bit operating systems? Data Representation

  28. Addressing and Byte Ordering • A variable x of type int (allocates 4 bytes) • If the address of x: 0x100 (means it starts storing from 0x100) • This means the 4 bytes of x would be stored in memory locations 0x100, 0x101, 0x102, and 0x103. • Let’s assume x has the value 0x1234567, which needs to be stored in four bytes. • There are two conventions to store the values in the 4 consecutive byte memory locations. 0x01, 0x23, 0x45, and 0x67, or 0x67, 0x45, 0x23, and 0x01, depending on CPU architecture. • Little endian byte order – Intel-compatible machines 0x103 0x102 0x101 0x100 address 0x01 0x23 0x45 0x67 value • Big endian byte order – machines from IBM and Sun Microsystems 0x103 0x102 0x101 0x100 0x67 0x45 0x23 0x01 Data Representation

  29. The byte orderings are totally invisible for most application programmers. • Why are the byte orderings important? • Think of data exchange between two machines through a network. • Assembly programming • When type casting is used Data Representation

  30. 2. Integer Representations C Java Size char, unsigned char byte 1B short, unsigned short short 2Bs char in Java uses 2Bs. int, unsigned intint 4Bs long, unsigned long long 8Bs // there is no unsigned in Java float float 4Bs double double 8Bs Data Representation

  31. Unsigned Encodings • unsigned char All 8 bits are used. No sign bit. • The smallest number is 0 • The maximum number is 0xff. • unsigned short 16 bits • The smallest number is ___ • The maximum number is ____ • unsigned int 32 bits • The smallest number is ___ • The maximum number is ____ • unsigned long 64 bits • The smallest number is ____ • The maximum number is _____ Data Representations

  32. Representation of Unsigned Integers • 8-bit representation of unsigned char 255 11111111 254 11111110 ... ... 128 10000000 127 01111111 126 01111110 ... ... 2 00000010 1 00000001 0 00000000 • The maximum number is ? • The minimum number is ? • What if we add the maximum number by 1 ??? • What if we subtract the minimum number by 1 ??? +1 +1 +1 overflow Data Representations

  33. Binary Addition • We will discuss binary addition and binary subtraction, before we discuss the representation of signed integers. • How to add two binary numbers? Let’s consider only unsigned integers(i.e., positive numbers only) for a while. • Just like the addition of two decimal numbers. • E.g., 10010 10010 1111 + 1001 + 1011 + 1 11011 11101 ??? 10111 + 111 ??? carry Data Representations

  34. Binary Subtraction • How to subtract a binary number? • Just like the subtraction of decimal numbers. • E.g., 0112 02 02 1000 10 10 10010 10010 10010 -1-1 -11 -11 -11 1 ?1 ?11 1111 Try: 101010 How to do? 1 -101-10 Data Representations

  35. In the previous slide, 10010 – 11 = 1111 • What if we add 00010010 + 11111100 1 00001110 + 1 00001111 • Is there any relationship between 112 and 111002? • The 1’s complement of 112 is ??? Switching 0  1 • This type of addition is called 1’s complement addition. • Find the 8-bit one’s complements of the followings. • 11011 -> 00011011 -> • 10 -> 00000010 -> • 101 -> 00000101 -> Data Representations

  36. In the previous slide, 10010 – 11 = 1111 • What if we add 00010010 + 11111101 1 00001111 • Is there any relationship between 11 and 11101? • The 2’s complement of 11 is ??? • 2’s complement ≡ 1’s complement + 1 -> 11100 + 1 = 11101 • This type of addition is called 2’s complement addition. • Find the 16-bit two’s complements of the followings. • 11011 -> 0000000000011011 -> • 10 • 101 Data Representations

  37. Another example 101010 - 101 ??? • What if we use 1’s complement addition or 2’s complement addition instead as follow? Let’s use 8-bit representation. 00101010 00101010 + 11111010+ 11111011 1 00100100 1 00100101 + 1 00100101 • What does this mean? • A – B = A + (–B), where A and B are positive • Is the 1’s complement or the 2’s complement of B sort of equal to –B? Data Representations

  38. Can we use 8-bit 1’s complement addition for 12 – 102 = –12? 1 00000001 - 10+ 11111101<- 8-bit 1’s complement of 10 11111110 <- Is this correct? (1’s complement of 1?) • Let’s use 8-bit 2’s complement addition for 12 – 102. 00000001 +11111110<- 2’s complement of 10 11111111 <- Correct? (2’s complement of 1?) • 12 – 102 = 12 + (–102) = –12 • How to represent negative binary numbers, i.e., signed integers? Data Representations

  39. Representation of Negative Binaries • Representation of signed integers • 8 or 16 or 32 bits are usually used for integers. • Let’s use 8 bits for examples. • The left most bit (called most significant bit) is used as sign. • When the MSB is 0, non-negative integers. • When the MSB is 1, negative integers. • The other 7 bits are used for integers. • How to represent positive integer 9? • 00001001 • How about -9? • 10001001 is really okay? • 00001001 (9) + 10001001 (-9) = 10010010 (-18) It is wrong! • We need a different representation for negative integers. Data Representations

  40. How about -9? • 10001001 is really okay? • 00001001 (9) + 10001001 (-9) = 10010010 (-18) It is wrong! • We need a different representation for negative integers. • What is the 8-bit 1’s complement of 9? • 11110110 <- 8-bit 1’s complement of 9 • 00001001 + 11110110 <- 9 + 8-bit 1’s complement of 9 = 11111111 <- Is it zero? (1’s complement of 0?) • What is the 2’s complement of 9? • 11110111 <- 8-bit 2’s complement of 9 • 00001001 + 11110111 <- 9 + 8-bit 2’s complement of 9 = 1 00000000 <- It looks like zero. • 2’s complement representation is used for negative integers. Data Representations

  41. 12 – 102 = 12 + (–102) ??? What is the result in decimal? 00000001 + 11111110<- 2’s complement of 10, i.e., -102 11111111 <- 2’s complement of 1, i.e., -1 (= 1 – 2) • 1010102 – 1012 = 1010102 + (–1012) ??? • 100102 – 112 ??? • 102 – 12 ??? • -102 – 12 ??? • Is the two’s complement of the two’s complement of an integer the same integer? • What is x when the 8-bit 2’s complement of x is • 11111111 11110011 10000001 Data Representations

  42. 8-bit representation of signed char with 2’s complement 127 01111111 126 01111110 ... ... 2 00000010 1 00000001 0 00000000 -1 11111111 -2 11111110 -3 11111101 ... ... -127 10000001 -128 10000000 • The maximum number is ? • The minimum number is ? • What if we add the maximum number by 1 ??? • What if we subtract the minimum number by 1 ??? overflow +1 +1 -1 overflow +1 -1 -1 Data Representations

  43. 16-bit representation signed short with 2’s complement ... 01111111 11111111 ... 01111111 11111110 ... ... 3 00000000 00000011 2 00000000 00000010 1 00000000 00000001 0 00000000 00000000 -1 11111111 11111111 -2 11111111 11111110 -3 11111111 11111101 ... ... ... 10000000 00000001 ... 10000000 00000000 • The maximum number is ? What if we add the maximum number by 1 ??? • The minimum number is ? What if we subtract the minimum number by 1 ??? overflow +1 +1 +1 -1 -1 -1 overflow Data Representations

  44. Note that computers use the 8-bit representation, the 16-bit representation, the 32-bit representation and the 64-bit representation with 2’complement for negative integers. • In programming languages • char, unsigned char 8-bits • short, unsigned short 16-bits • int, unsigned int 32-bits • long, unsigned long 64-bits • When we use the 32-bit representation with 2’s complement, • The maximum number is ? • What if we add the maximum number by 1 ??? • The minimum number is ? • What if we subtract the minimum number by 1 ??? Data Representations

  45. Now we know how to represent negative integers. • 2’ complement addtion A + (–B) is computed for subtraction A – B. • Let’s suppose B is negative. Then –B is really a positive integer? For example, let’s consider 1 byte signed integer. 127 01111111 126 01111110 ... ... 2 00000010 1 00000001 0 00000000 -1 11111111 -2 11111110 -3 11111101 2’s complement of -3 is 00000011, i.e., 3. ... ... -127 10000001 -128 10000000 2’s complement of -128 is 10000000 again. • For any -127 < x < 127, x – x = 0. But (-128) – (-128) = ??? Data Representation

  46. Binary Multiplication • Bit by bit 1 0 1 1 1 x 1 0 1 0 0 0 0 0 0 1 0 1 1 1 0 0 0 0 0 1 0 1 1 1 1 1 1 0 0 1 1 0 Data Representation

  47. Binary Division • Binary division? 1111 <- quotient 101 1001101 -101 1001 -101 1000 -101 111 -101 10 <- remainder • Try 1101011 / 110 • Dividing negative binary numbers: division without sign, and then put the sign. This is why division is an expensive operation. Number Systems

  48. Binary division by a power of 2? 1001101 / 10 = 100110 1001101 / 100 = 10011 1001101 / 1000 = 1001 • What if we shift 1001101 right by 1 bit? • What if we shift 1001101 right by 2 bits? • What if we shift 1001101 right by 3 bits? • 1001101 / 101 ??? Complicated implementation required Number Systems

  49. 2.4 Floating Point • Fractional binary numbers • IEEE floating-point representation Data Representation

  50. 2-1 = 0.5 2-2 = 0.25 2-3 = 0.125 00101000.101 (40.625) + 11111110.110(-1.25) (2’s complement) 00100111.011 (39.375) Fractions: Fixed-Point • How can we represent fractions? • Use “binary point” to separate positive from negative powers of two -- like “decimal point.” • 2’s comp addition and subtraction still work. (Assuming binary points are aligned) No new operations -- same as integer arithmetic. Number Systems

More Related