today s topics
play

Todays Topics ! Strings ! Boolean algebra ! Representation of - PowerPoint PPT Presentation

University of Washington Todays Topics ! Strings ! Boolean algebra ! Representation of integers: unsigned and signed ! Casting ! Arithmetic and shifting ! Sign extension 1 University of Washington Quick review x at location 0x04, y at


  1. University of Washington Today’s Topics ! Strings ! Boolean algebra ! Representation of integers: unsigned and signed ! Casting ! Arithmetic and shifting ! Sign extension 1

  2. University of Washington Quick review… x at location 0x04, y at 0x18 int * x; int y; x = &y + 3; // get address of y add 12 int * x; int y; 0000 *x = y; // value of y to location x points 0004 0008 000C 0010 0014 AA BB CC DD 0018 001C 0020 0024 2

  3. University of Washington Representing strings? 3

  4. University of Washington Representing strings A C-style string is represented by an array of bytes. — Elements are one-byte ASCII codes for each character. — A 0 value marks the end of the array. 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 4

  5. University of Washington Null-terminated Strings For example, “Harry Potter” can be stored as a 13-byte array. 72 97 114 114 121 32 80 111 116 116 101 114 0 H a r r y P o t t e r \0 Why do we put a a 0, or null, at the end of the string? Computing string length?

  6. University of Washington Compatibility Linux/Alpha S Sun S 31 31 32 32 33 33 34 34 35 35 00 00 Byte ordering not an issue Unicode characters – up to 4 bytes/character ASCII codes still work (leading 0 bit) but can support the many characters in all languages in the world Java and C have libraries for Unicode (Java commonly uses 2 bytes/ char) 6

  7. University of Washington Boolean Algebra Developed by George Boole in 19th Century Algebraic representation of logic Encode “True” as 1 and “False” as 0 AND: A&B = 1 when both A is 1 and B is 1 OR: A|B = 1 when either A is 1 or B is 1 XOR: A^B = 1 when either A is 1 or B is 1, but not both NOT: ~A = 1 when A is 0 and vice-versa DeMorgan’s Law: ~(A | B) = ~A & ~B 7

  8. University of Washington General Boolean Algebras Operate on bit vectors Operations applied bitwise 01101001 01101001 01101001 & 01010101 | 01010101 ^ 01010101 ~ 01010101 01000001 01111101 00111100 10101010 All of the properties of Boolean algebra apply 01010101 ^ 01010101 00111100 How does this relate to set operations? 8

  9. University of Washington Representing & Manipulating Sets Representation Width w bit vector represents subsets of {0, …, w –1} a j = 1 if j ! A { 0, 3, 5, 6 } 01101001 76543210 { 0, 2, 4, 6 } 01010101 76543210 Operations & Intersection 01000001 { 0, 6 } | Union 01111101 { 0, 2, 3, 4, 5, 6 } ^ Symmetric difference 00111100 { 2, 3, 4, 5 } ~ Complement 10101010 { 1, 3, 5, 7 } 9

  10. University of Washington Bit-Level Operations in C Operations &, |, ^, ~ are available in C Apply to any “integral” data type long , int , short , char, unsigned View arguments as bit vectors Arguments applied bit-wise Examples (char data type) ~0x41 --> 0xBE ~01000001 2 --> 10111110 2 ~0x00 --> 0xFF ~00000000 2 --> 11111111 2 0x69 & 0x55 --> 0x41 01101001 2 & 01010101 2 --> 01000001 2 0x69 | 0x55 --> 0x7D 01101001 2 | 01010101 2 --> 01111101 2 10

  11. University of Washington Contrast: Logic Operations in C Contrast to logical operators && , || , ! View 0 as “False” Anything nonzero as “True” Always return 0 or 1 Early termination Examples (char data type) !0x41 --> 0x00 !0x00 --> 0x01 !!0x41 --> 0x01 0x69 && 0x55 --> 0x01 0x69 || 0x55 --> 0x01 ( avoids null pointer access, null pointer = 0x00000000 ) p && *p++ 11

  12. University of Washington Encoding Integers ! The hardware (and C) supports two flavors of integers: unsigned – only the non-negatives ! signed – both negatives and non-negatives ! ! There are only 2 W distinct bit patterns of W bits, so... Can't represent all the integers ! Unsigned values are 0 ... 2 W -1 ! Signed values are -2 W-1 ... 2 W-1 -1 !

  13. University of Washington Unsigned Integers ! Unsigned values are just what you expect b 7 b 6 b 5 b 4 b 3 b 2 b 1 b 0 = b 7 2 7 + b 6 2 6 + b 5 2 5 + … + b 1 2 1 + b 0 2 0 ! - Interesting aside: 1+2+4+8+...+2 N-1 = 2 N -1 00111111 63 +00000001 + 1 01000000 64 ! You add/subtract them using the normal “carry/borrow” rules, just in binary ! An important use of unsigned integers in C is pointers There are no negative memory addresses !

  14. University of Washington Signed Integers ! Let's do the natural thing for the positives They correspond to the unsigned integers of the same ! value - Example (8 bits): 0x00 = 0, 0x01 = 1, …, 0x7F = 127 ! But, we need to let about half of them be negative Use the high order bit to indicate 'negative' ! Call it “the sign bit” ! Examples (8 bits): ! - 0x00 = 00000000 2 is non-negative, because the sign bit is 0 - 0x7F = 01111111 2 is non-negative - 0x80 = 10000000 2 is negative

  15. University of Washington Sign-and-Magnitude Negatives ! How should we represent -1 in binary? Possibility 1: 10000001 2 ! Use the MSB for “+ or -”, and the other bits to give magnitude – 7 + 0 – 6 + 1 1111 0000 1110 0001 – 5 + 2 1101 0010 – 4 + 3 1100 0011 1011 0100 – 3 + 4 1010 0101 – 2 + 5 1001 0110 1000 0111 – 1 + 6 – 0 + 7

  16. University of Washington Sign-and-Magnitude Negatives ! How should we represent -1 in binary? Possibility 1: 10000001 2 ! Use the MSB for “+ or -”, and the other bits to give magnitude (Unfortunate side effect: there are two representations of 0!) – 7 + 0 – 6 + 1 1111 0000 1110 0001 – 5 + 2 1101 0010 – 4 + 3 1100 0011 1011 0100 – 3 + 4 1010 0101 – 2 + 5 1001 0110 1000 0111 – 1 + 6 – 0 + 7

  17. University of Washington Sign-and-Magnitude Negatives ! How should we represent -1 in binary? Possibility 1: 10000001 2 ! Use the MSB for “+ or -”, and the other bits to give magnitude Another problem: math is cumbersome – 7 + 0 4 – 3 != 4 + (-3) – 6 + 1 1111 0000 1110 0001 – 5 + 2 1101 0010 – 4 + 3 1100 0011 1011 0100 – 3 + 4 1010 0101 – 2 + 5 1001 0110 1000 0111 – 1 + 6 – 0 + 7

  18. University of Washington Ones’ Complement Negatives ! How should we represent -1 in binary? Possibility 2: 11111110 2 ! Negative numbers: bitwise complements of positive numbers It would be handy if we could use the same hardware adder – 0 + 0 to add signed integers as unsigned – 1 + 1 1111 0000 1110 0001 – 2 + 2 1101 0010 – 3 + 3 1100 0011 1011 0100 – 4 + 4 1010 0101 – 5 + 5 1001 0110 1000 0111 – 6 + 6 – 7 + 7

  19. University of Washington Ones’ Complement Negatives ! How should we represent -1 in binary? Possibility 2: 11111110 2 ! Negative numbers: bitwise complements of positive numbers - Solves the arithmetic problem end-around carry

  20. University of Washington Ones’ Complement Negatives ! How should we represent -1 in binary? Possibility 2: 11111110 2 ! Negative numbers: bitwise complements of positive numbers Use the same hardware adder to add signed integers as unsigned (but we have to keep track of the end-around carry bit) Why does it work? The ones’ complement of a 4-bit positive number y • is 1111 2 – y • 0111 ! 7 10 • 1111 2 – 0111 2 = 1000 2 ! –7 10 1111 2 is 1 less than 10000 2 = 2 4 – 1 • • – y is represented by (2 4 – 1) – y

  21. University of Washington Ones’ Complement Negatives ! How should we represent -1 in binary? Possibility 2: 11111110 2 ! Negative numbers: bitwise complements of positive numbers (But there are still two representations of 0!) – 0 + 0 – 1 + 1 1111 0000 1110 0001 – 2 + 2 1101 0010 – 3 + 3 1100 0011 1011 0100 – 4 + 4 1010 0101 – 5 + 5 1001 0110 1000 0111 – 6 + 6 – 7 + 7

  22. University of Washington Two's Complement Negatives ! How should we represent -1 in binary? Possibility 3: 11111111 2 ! Bitwise complement plus one (Only one zero) – 1 0 – 2 + 1 1111 0000 1110 0001 – 3 + 2 1101 0010 – 4 + 3 1100 0011 1011 0100 – 5 + 4 1010 0101 – 6 + 5 1001 0110 1000 0111 – 7 + 6 – 8 + 7

  23. University of Washington Two's Complement Negatives ! How should we represent -1 in binary? Possibility 3: 11111111 2 ! Bitwise complement plus one (Only one zero) Simplifies arithmetic ! Use the same hardware adder to add signed integers as unsigned (simple addition; discard the highest carry bit)

  24. University of Washington Two's Complement Negatives ! How should we represent -1 in binary? Two’s complement: Bitwise complement plus one ! Why does it work? Recall: The ones’ complement of a b -bit positive number y • is (2 b – 1) – y Two’s complement adds one to the bitwise complement, • thus, - y is 2 b – y • – y and 2 b – y are equal mod 2 b (have the same remainder when divided by 2 b ) • Ignoring carries is equivalent to doing arithmetic mod 2 b

Recommend


More recommend