data representation
play

Data Representation January 914, 2013 1 / 40 Quick logistical - PowerPoint PPT Presentation

Data Representation January 914, 2013 1 / 40 Quick logistical notes In class exercises Bring paper and pencil (or laptop) to each lecture! Goals: break up lectures, keep you engaged chance to work through problems in class ask


  1. Data Representation January 9–14, 2013 1 / 40

  2. Quick logistical notes In class exercises Bring paper and pencil (or laptop) to each lecture! Goals: • break up lectures, keep you engaged • chance to work through problems in class • ask questions! First homework will be posted before Friday’s lecture! 2 / 40

  3. Outline Internal vs. external representations Representing the natural numbers Binary number system Binary arithmetic Hexadecimal and base-N number systems Fixed-size integer representations Representing negative numbers Big endian vs. little endian 3 / 40

  4. Internal vs. external representations Internal representation How the data is actually represented in the computer hardware External representation How we interpret or conceptualize the internal representation 4 / 40

  5. Internal representations Usually two states, which we interpret as 0 and 1 Volatile representations: • Capacitor (DRAM) • charged or not • Flip-flop circuit (SRAM) • one of two output signals is high Non-volatile representations: • Region of a magnetized surface (hard disks, tape) • positive or negative • Floating gate transistor (flash) • change in voltage • one cell can represent more than two states! • e.g. one 16-level cell ≈ four flip-flops 5 / 40

  6. Interacting with the internal representation Architecture provides an interface • can interact with the internal representation • using the abstraction of the external representation Advantages: • Don’t have to think about internal representation • Architecture can be implemented by different hardware 6 / 40

  7. Organization of the internal representation Usually can’t refer to individual bits • Internal representation organized into groups • Through ISA, can read/write a group by an address Addressable groups in MIPS • byte = 8 bits • word = 4 bytes = 32 bits • (also halfword = 2 bytes = 16 bits) 7 / 40

  8. External representations Conceptually, view data as a sequence of 0 s and 1 s The same data can be interpreted in different ways: Example: 1111 0110 ö extended ASCII character 246 unsigned integer − 10 signed 8-bit integer 8 / 40

  9. Outline Internal vs. external representations Representing the natural numbers Binary number system Binary arithmetic Hexadecimal and base-N number systems Fixed-size integer representations Representing negative numbers Big endian vs. little endian 9 / 40

  10. Decimal number system (base 10) How it works (positional number system): • 10 digits, used in sequence • each position corresponds to a power of 10 • sum of each digit multiplied by position value Example: 2037 10 5 10 4 10 3 10 2 10 1 10 0 . . . 100 , 000 10 , 000 1000 100 10 1 . . . ( 0 ) ( 0 ) 2 0 3 7 2 · 1000 + 0 · 100 + 3 · 10 + 7 · 1 = 2000 + 0 + 30 + 7 = 2037 10 / 40

  11. Binary number system (base 2) Works the same way! • 2 bits, used in sequence ( bi nary digi t ) • each position corresponds to a power of 2 • sum of each bit multiplied by position value Example: 110101 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 . . . 128 64 32 16 8 4 2 1 . . . ( 0 ) ( 0 ) 1 1 0 1 0 1 1 · 32 + 1 · 16 + 0 · 8 + 1 · 4 + 0 · 2 + 1 · 1 = 32 + 16 + 0 + 4 + 0 + 1 = 53 11 / 40

  12. Converting from binary to decimal Very easy: • Since binary is just 0 s and 1 s, no need to multiply • Just add up the position values of the 1 bits Example: 1011 0010 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 . . . 128 64 32 16 8 4 2 1 . . . 1 0 1 1 0 0 1 0 128 + 32 + 16 + 2 = 178 12 / 40

  13. Converting from decimal to binary Method 1: Subtracting powers of 2 For each position p from left to right • If 2 p ≤ n , subtract and write 1 • Otherwise, write 0 Example: 157 157 − 128 = 29 1 for 128’s position 29 − 16 = 13 0 for 64, 0 for 32, 1 for 16 13 − 8 = 5 1 for 8 5 − 4 = 1 1 for 4 1 − 1 = 0 0 for 2, 1 for 1 1001 1101 13 / 40

  14. Converting from decimal to binary Method 2: Successive division by 2 • Divide by 2 until you reach 0, keeping track of remainders • Write the remainders, from last to first Example: 157 157 ÷ 2 = 78 R 1 78 ÷ 2 = 39 R 0 39 ÷ 2 = 19 R 1 19 ÷ 2 = 9 R 1 1001 1101 9 ÷ 2 = 4 R 1 4 ÷ 2 = 2 R 0 2 ÷ 2 = 1 R 0 1 ÷ 2 = 0 R 1 14 / 40

  15. In class exercises Convert from binary to decimal: • 0010 1010 • 1001 0101 Convert from decimal to binary: • 169, by subtracting powers of 2 • 84, by successive division by 2 15 / 40

  16. Binary addition Just like adding decimal numbers! To add two binary numbers • Pairwise add each bit, starting from the right • 0 + 0 = 0 and 0 + 1 = 1 • On 1 + 1, carry a bit to the left Example: 0110 + 0011 Example: 0011 + 0011 11 11 0110 0011 + 0011 + 0011 1001 0110 16 / 40

  17. Binary multiplication Same algorithm as decimal (only easier) To multiply two binary numbers A and B 1. For each bit b in B : • Multiply b × A , aligning the result with b (since b is 0 or 1, each step yields 0 or a A !) 2. Sum the results Example: 1101 × 1101 1101 11111 1. 2. × 1101 1101 Often easiest to add 0000 1101 results two at a time 110100 0 + 1101000 1101 1101 10101001 17 / 40

  18. Special case: multiplying by a power of 2 Super easy, just like multiplying by powers of 10 in decimal To multiply a binary number by 2 p Add p 0s on the right Examples • 100 × 1101 = 110100 • 1010 × 1000 = 1010000 18 / 40

  19. Hexadecimal number system (base 16) Very useful for representing binary data concisely! • 16 digits: 0–9, A, B, C, D, E, F • each position corresponds to a power of 16 • usually prefixed with 0x Each hex digit corresponds to 4 bits 0 0000 4 0100 8 1000 C 1100 1 0001 5 0101 9 1001 D 1101 2 0010 6 0110 A 1010 E 1110 3 0011 7 0111 B 1011 F 1111 One byte = 2 hex digits 19 / 40

  20. Converting hexadecimal ⇔ binary Each hex digit corresponds to 4 bits 0 0000 4 0100 8 1000 C 1100 1 0001 5 0101 9 1001 D 1101 2 0010 6 0110 A 1010 E 1110 3 0011 7 0111 B 1011 F 1111 Examples • 0xA4F7 = 1010 0100 1111 0111 • 0x0B60 = 0000 1011 0110 0000 We will be doing this a lot this quarter. :) 20 / 40

  21. Converting hexadecimal ⇔ decimal Two strategies: • Convert directly • Convert hexadecimal ⇔ binary ⇔ decimal Example: 0xB6A4 (direct conversion) 16 4 16 3 16 2 16 1 16 0 . . . 65 , 536 4 , 096 256 16 1 . . . ( 0 ) B 6 A 4 11 · 4096 + 6 · 256 + 10 · 16 + 4 · 1 = 45056 + 1536 + 160 + 4 = 46 , 756 21 / 40

  22. Representation in other bases In general, we can represent numbers in any base Some other significant bases: • Base 8 — octal • each octal digit is equivalent to three bits (000 = 0 8 , 001 = 1 8 , 010 = 2 8 , . . . , 111 = 7 8 ) • useful in old architectures with 12, 24, 36 bit words • support in C and many assembly languages (071 = 71 8 = 53 10 ) • Base 64 (0–9, A–Z, a–z, +, /) • each base-64 digit is equivalent to six bits • used in MIME to transmit binary data in plain ASCII text 22 / 40

  23. In class exercises Add in binary: • 100 1100 + 1110 1111 Multiply in binary: • 1011 × 101 Add in hexadecimal: • 0x28 + 0x4A 0 0000 4 0100 8 1000 C 1100 1 0001 5 0101 9 1001 D 1101 2 0010 6 0110 A 1010 E 1110 3 0011 7 0111 B 1011 F 1111 23 / 40

  24. Outline Internal vs. external representations Representing the natural numbers Binary number system Binary arithmetic Hexadecimal and base-N number systems Fixed-size integer representations Representing negative numbers Big endian vs. little endian 24 / 40

  25. Arbitrary vs. fixed precision So far, we have been assuming arbitrary precision • to represent a bigger number, just add more bits/digits! In practice, integers have a fixed size • commonly 32 or 64 bits • based on register size of the architecture This is significant for two reasons: • risk of overflow • representation of negative numbers 25 / 40

  26. Representing negative numbers Must first specify the fixed size of the integer! • With n bits, we can represent 2 n different values • Idea: split space so half the values represent negatives Sign and magnitude representation • First bit represents the sign (0 positive, 1 negative) • Rest of bits represent the magnitude , that is | x | Suppose 4-bit integers • Examples: − 1 = 1001 − 4 = 1100 − 7 = 1111 This is exactly the representation you’re used to in decimal! 26 / 40

  27. Problems with sign and magnitude representation This turns out to not be a very good representation . . . why? Issue 1: Multiple zeros • Both 0000 and 1000 represent the same value • This is strange and requires extra effort Issue 2: Complicated arithmetic Simple binary addition doesn’t work 0 010 1 010 0 010 ✓ ✓ ✗ + 0 011 + 1 011 + 1 011 0 101 1 101 1 001 27 / 40

  28. One’s complement representation One’s complement • start with the fixed-size binary representation of | x | • invert every bit Features: • Binary addition is simple (wrap-around carry) • Still two zeros (all 0s and all 1s) Examples • -2 1. 0010 2. 1101 • -3 1. 0011 2. 1100 • -5 1. 0101 2. 1010 28 / 40

  29. One’s complement addition Overflow carries “wrap around” (added on the right) Example: − 2 + − 3 = − 5 11 1101 + 1100 1001 + 1 1010 29 / 40

Recommend


More recommend