integer representation
play

Integer Representation Bits, binary numbers, and bytes Fixed-width - PowerPoint PPT Presentation

Wellesley CS 240 Integer Representation Bits, binary numbers, and bytes Fixed-width representation of integers: unsigned and signed Modular arithmetic and overflow 1 positional number representation 2 4 0 = 2 x 10 2 + 4 x 10 1 + 0 x 10 0


  1. Wellesley CS 240 Integer Representation Bits, binary numbers, and bytes Fixed-width representation of integers: unsigned and signed Modular arithmetic and overflow 1

  2. positional number representation 2 4 0 = 2 x 10 2 + 4 x 10 1 + 0 x 10 0 100 10 1 weight 10 2 10 1 10 0 position 2 1 0 • Base determines: – – • Each position holds a digit. • Represented value = 3

  3. binary = base 2 1 0 1 1 = 1 x 2 3 + 0 x 2 2 + 1 x 2 1 + 1 x 2 0 8 4 2 1 weight 2 3 2 2 2 1 2 0 position 3 2 1 0 When ambiguous, subscript with base: 101 10 Dalmatians (movie) 101 ten 101 2 -Second Rule (folk wisdom for food safety) 101 two irony 4

  4. ex Powers of 2: memorize up to ≥ 2 10 (in base ten)

  5. Show powers, strategies. ex conversion and arithmetic 19 10 = ? 2 1001 2 = ? 10 240 10 = ? 2 11010011 2 = ? 10 101 2 + 1011 2 = ? 2 1001011 2 x 2 10 = ? 2 7

  6. numbers and wires One wire carries one bit. How many wires to represent a given number? 1 0 0 1 1 0 0 0 1 0 0 1 What if I want to build a computer (and not change the hardware later)?

  7. What do you call 4 bits? byte = 8 bits a.k.a. octet Smallest unit of data 0 0 0000 used by a typical modern computer 1 1 0001 2 2 0010 Binary 00000000 2 -- 11111111 2 3 3 0011 4 4 0100 Decimal 000 10 -- 255 10 5 5 0101 Hexadecimal 00 16 -- FF 16 6 6 0110 7 7 0111 8 8 1000 9 9 1001 Programmer’s hex notation (C, etc.): A 10 1010 B 11 1011 0xB4 = B4 16 = B4 hex C 12 1100 Octal (base 8) also useful. D 13 1101 Why do 240 students often confuse Halloween and Christmas? E 14 1110 F 15 1111 9

  8. word |wərd| , n. Natural (fixed size) unit of data used by processor. – Word size determines: 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 0 0 0 0 0 0 0 0 1 0 1 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 MSB: most significant bit LSB: least significant bit 10

  9. fixed-size data representations (size in bytes ) Java Data Type C Data Type 32-bit word 64-bit word boolean 1 1 byte char 1 1 char 2 2 short short int 2 2 int int 4 4 float float 4 4 long int 4 8 double double 8 8 long long long 8 8 long double 8 16 Depends on word size 11

  10. char : representing characters A C-style string is represented by a series of bytes ( char s ). — One-byte ASCII codes for each character. — ASCII = American Standard Code for Information Interchange 32 space 48 0 64 @ 80 P 96 ` 112 p 33 ! 49 1 65 A 81 Q 97 a 113 q 34 50 2 66 B 82 R 98 b 114 r ” 35 # 51 3 67 C 83 S 99 c 115 s 36 $ 52 4 68 D 84 T 100 d 116 t 37 % 53 5 69 E 85 U 101 e 117 u 38 & 54 6 70 F 86 V 102 f 118 v 39 55 7 71 G 87 W 103 g 119 w ’ 40 ( 56 8 72 H 88 X 104 h 120 x 41 ) 57 9 73 I 89 Y 105 I 121 y 42 * 58 : 74 J 90 Z 106 j 122 z 43 + 59 ; 75 K 91 [ 107 k 123 { 44 , 60 < 76 L 92 \ 108 l 124 | 45 - 61 = 77 M 93 ] 109 m 125 } 46 . 62 > 78 N 94 ^ 110 n 126 ~ 47 / 63 ? 79 O 95 _ 111 o 127 del

  11. Fixed-width integer encodings Unsigned non-negative integers only ⊂ Signed both negative and non-negative integers ⊂ n bits offer only 2 n distinct values. Terminology: “Most-significant” bit(s) “Least-significant” bit(s) or “high-order” bit(s) or “low-order” bit(s) 0110010110101001 MSB LSB 13

  12. (4-bit) unsigned integer representation 1 0 1 1 = 1 x 2 3 + 0 x 2 2 + 1 x 2 1 + 1 x 2 0 8 4 2 1 weight 2 3 2 2 2 1 2 0 position 3 2 1 0 n -bit unsigned integers: minimum = maximum = 14

  13. modular arithmetic, overflow 15 0 13 1101 1011 11 14 1 1111 0000 + 5 + 0101 1110 0001 + 0010 13 2 + 2 1101 0010 12 3 4-bit 1100 0011 unsigned 1011 integers 0100 11 4 1010 0101 10 5 1001 0110 1000 0111 9 6 8 7 x+y in n -bit unsigned arithmetic is (x + y) mod 2 N in math unsigned overflow = "wrong" answer = wrap-around = carry 1 out of MSB = math answer too big to fit Unsigned addition overflows if and only if a carry bit is dropped. 15

  14. !!! sign-magnitude Most-significant bit (MSB) is sign bit 0 means non-negative 1 means negative Remaining bits are an unsigned magnitude 8-bit sign-magnitude: Anything weird here? 0 0000000 represents _____ Arithmetic? Example: 0 1111111 represents _____ 4 - 3 != 4 + (-3) 1 0000101 represents _____ 00000100 +10000011 1 0000000 represents _____ ex Zero? 17

  15. (4-bit) two's complement signed integer representation 1 0 1 1 = 1 x -2 3 + 0 x 2 2 + 1 x 2 1 + 1 x 2 0 -2 3 2 2 2 1 2 0 4 -bit two's complement integers: minimum = maximum = 19

  16. two’s complement vs. unsigned _ _ …_ _ _ unsigned places 2 n -1 2 n -2 … 2 2 2 1 2 0 - 2 n -1 2 n -2 … 2 2 2 1 2 0 two's complement What's the difference? places n -bit minimum = n -bit maximum = 20

  17. ex 8-bit representations 0 0 0 0 1 0 0 1 1 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 1 0 0 1 1 1 n-bit two's complement numbers: minimum = maximum = 21

  18. 4-bit unsigned vs. 4-bit two’s complement 1 0 1 1 1 x 2 3 + 0 x 2 2 + 1 x 2 1 + 1 x 2 0 1 x -2 3 + 0 x 2 2 + 1 x 2 1 + 1 x 2 0 11 -5 difference = ___ = 2 ___ – 1 0 15 0 – 2 + 1 14 1 1111 0000 1111 0000 1110 0001 1110 0001 – 3 + 2 13 2 1101 0010 1101 0010 – 4 4-bit + 3 12 3 1100 0011 1100 0011 4-bit two's unsigned complement 1011 0100 1011 0100 – 5 + 4 11 4 1010 0101 1010 0101 10 5 – 6 + 5 1001 0110 1001 0110 1000 0111 1000 0111 – 7 + 6 9 6 – 8 + 7 8 7 22

  19. two’s complement addition -2 2 0010 1110 + -3 + 3 + 0011 + 1101 – 1 0 – 2 + 1 1111 0000 1110 0001 – 3 + 2 1101 0010 – 4 + 3 1100 0011 0010 2 -2 1110 1011 0100 – 5 + 4 1010 0101 + 1101 + -3 + 3 + 0011 – 6 + 5 1001 0110 1000 0111 – 7 + 6 – 8 + 7 Modular Arithmetic 23

  20. two’s complement overflow Addition overflows if and only if the arguments have the same sign but the result does not. if and only if the carry in and carry out of the sign bit differ. – 1 0 -1 1111 – 2 + 1 1111 0000 + 2 + 0010 1110 0001 – 3 + 2 1101 0010 – 4 + 3 1100 0011 1011 0100 – 5 + 4 1010 0101 – 6 + 5 1001 0110 6 0110 1000 0111 – 7 + 6 + 3 + 0011 – 8 + 7 Modular Arithmetic Some CPUs/languages raise exceptions on overflow. C and Java cruise along silently... Feature? Oops? 24

  21. Reliability Ariane 5 Rocket, 1996 Exploded due to cast of 64-bit floating-point number to 16-bit signed number. Overflow. "... a Model 787 airplane … can lose all Boeing 787, 2015 alternating current (AC) electrical power … caused by a software counter internal to the GCUs that will overflow after 248 days of continuous power. We are issuing this AD to prevent loss of all AC electrical power, which could result in loss of control of the airplane ." --FAA, April 2015

  22. A few reasons two’s complement is awesome Addition, subtraction, hardware Sign Negative one Complement rules

  23. ex Another derivation How should we represent 8-bit negatives? • For all positive integers x , x and –x should sum to zero. • Use the standard addition algorithm. 00000001 00000010 00000011 + + + 00000000 00000000 00000000 • Find a rule to represent –x where that works… 27

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend