week 2 to do mathematics 3670 computer systems bits data
play

Week 2: to do Mathematics 3670: Computer Systems Bits, Data Types, - PDF document

Week 2: to do Mathematics 3670: Computer Systems Bits, Data Types, and Operations What When Read Chapter 2 this week Design Lab 2 TMs before Thursday Dr. Andrew Mertz Complete Lab 2 this Thursday Mathematics and Computer Science


  1. Week 2: to do Mathematics 3670: Computer Systems Bits, Data Types, and Operations What When Read Chapter 2 this week Design Lab 2 TMs before Thursday Dr. Andrew Mertz Complete Lab 2 this Thursday Mathematics and Computer Science Department Submit Lab 2 work by next Thursday Eastern Illinois University Fall 2012 Geek humor 3-bit codes: no assigned meaning 000 111 001 0 0 0 0 0 1 0 1 0 0 1 1 110 010 1 0 0 1 0 1 1 1 0 1 1 1 101 011 100 Source: http://xkcd.com/571/ 3 bits yield 2 3 = 8 possibilities Number of bit patterns Representations number of bits number of bit patterns 2 3 = 8 3 What is the meaning of 0011010111110010 ? 2 4 = 16 4 . . . . . . 2 m An integer? If so, which representation? m . . . . . . One or more characters? (ASCII or Unicode) 2 16 = 65 , 536 16 A floating point value? . . . . . . 2 32 = 4 , 294 , 967 , 296 32 A value of an enumeration type? . . . . . . Something else? 2 64 = 18 , 446 , 744 , 073 , 709 , 551 , 616 64 . . . . . .

  2. Shorthand notation: hexadecimal ASCII code A merican S tandard C ode for I nformation I nterchange Consider a bit string such as: 0011010111110010 Use 4-bit groups 0011 0101 1111 0010 ASCII uses a 7-bit code Use hexadecimal digits: 3 5 F 2 7 bits allows for only 2 7 = 128 different characters pattern 0000–1001 1010 1011 1100 1101 1110 1111 hexadecimal 0–9 A B C D E F See http://highered.mcgraw-hill.com/sites/dl/free/ 0072467509/104653/PattPatelAppE.pdf Unicode Wheel of 3-bit codes: food choices (enumeration type) 000 banana 111 001 One system for all the world’s languages mango papaya Unicode uses a muti-byte code 110 010 2 bytes provides 2 16 = 65 , 536 different characters apricot apple See http://www.unicode.org/charts/ 101 011 pear orange 100 peach Wheel of 3-bit codes: unsigned integers Wheel of 3-bit codes: signed magnitude integers 000 000 0 0 111 001 111 001 − 3 1 7 1 leading bit: sign 110 010 110 010 +0 and − 0 − 2 2 6 2 Symmetric range [ − 3 , +3] 101 011 101 011 − 1 3 5 3 100 100 − 0 4

  3. Wheel of 3-bit codes: one’s complement integers Wheel of 3-bit codes: two’s complement, signed integers 000 000 0 0 111 001 111 001 − 0 1 − 1 1 asymmetric range [ − 4 , +3] +0 and − 0 economical: 110 110 010 010 subtraction via Symmetric range − 1 − 2 2 2 addition [ − 3 , +3] explains counting sheep comic 101 011 101 011 − 2 3 − 3 3 100 100 − 3 − 4 Wheel of m -bit codes: two’s complement, signed integers Lab 2 exercise: complement and add one n input 0 0 1 1 0 0 1 0 0 0 − 1 1 2 − 2 ... . ⇓ . . 0 000. . . 000 complement 1 0 0 1 1 0 1 1 1 1 000. . . 001 ⇓ 2 000. . . 010 TC ( n ) add one 1 0 0 1 1 1 0 0 0 . . . . . . 2 m − 1 − 1 Consider n + TC ( n ) . . . 011. . . 111 − 2 m − 1 100. . . 000 n 0 1 1 0 0 1 0 0 0 . . . . . . TC ( n ) 1 0 0 1 1 1 0 0 0 − 2 111. . . 110 0 0 0 0 0 0 0 0 0 − 1 111. . . 111 TC ( n ) is the additive inverse of n : i.e., n + TC ( n ) = 0 2 m − 1 − 1 − 2 m − 1 Two’s complement addition Two’s complement: example Let m = b n − 1 b n − 2 . . . b 2 b 1 b 0 be an arbitrary n -bit pattern Using an 8-bit register, what is the two’s complement Complement each bit: C ( m ) = b n − 1 b n − 2 . . . b 2 b 1 b 0 representation of − 20 ? 20 = 16 + 4 m + TC ( m ) = m + ( C ( m ) + 1) = 0 0 0 1 0 1 0 0 = ( m + C ( m )) + 1 complement → 1 1 1 0 1 0 1 1 add one → 1 1 1 0 1 1 0 0 = ( b n − 1 b n − 2 . . . b 2 b 1 b 0 + b n − 1 b n − 2 . . . b 2 b 1 b 0 ) + 1 Verify. . . = (11 . . . 111) + 1 n 0 0 0 1 0 1 0 0 = 00 . . . 000 TC ( n ) 1 1 1 0 1 1 0 0 Conclusion If m represents an integer k , then TC ( m ) represents − k .

  4. Two’s complement: example Two’s complement: binary to decimal conversion Given a bit string n = b w − 1 b w − 2 . . . b 2 b 1 b 0 , what value is Using an 8-bit register, what is TC ( − 20) ? represented? − 20 → 1 1 1 0 1 1 0 0 MSB? Conclusion What to do complement → 0 0 0 1 0 0 1 1 b w − 1 = 0 value is non-negative evaluate n as a binary value add one → 0 0 0 1 0 1 0 0 b w − 1 = 1 value is negative find TC ( n ) , evaluate, affix sign = 16 + 4 TC ( − 20) = 20 0 1 1 0 0 1 0 0 0 = ? 1 0 0 1 1 1 0 0 0 = ? Two’s complement: decimal to binary conversion Decimal to binary conversion (non-negative value) Give a decimal value n and a word length w , what bit string convert( n ) b w − 1 b w − 2 . . . b 2 b 1 b 0 represents n ? Successive division by two generates the bits in reverse order, from LSB to MSB. For example, take n = 57 : Sign? What to do n ≥ 0 convert( n ) 57 ÷ 2 = 28 × 2 + 1 n < 0 TC(convert( | n | )) 28 ÷ 2 = 14 × 2 + 0 14 ÷ 2 = 7 × 2 + 0 To convert a non-negative value. . . 7 ÷ 2 = 3 × 2 + 1 3 ÷ 2 = 1 × 2 + 1 Greedy “brute force” algorithm identify highest powers of two 1 ÷ 2 = 0 × 2 + 1 Successive division by two identify bits from LSB to MSB Examples: Using an 8-bit word. . . 57 = ? Conclusion: (57) 10 = (111001) 2 . For an 8-bit register, we fill with − 57 = ? leading zeros: (57) 10 = (00111001) 2 . Decimal to binary conversion (negative value) Addition on binary quantities TC(convert( | n | )) What is the 8-bit, two’s complement representation of n = − 57 ? c 4 c 3 c 2 c 1 convert ( | n | ) = convert (57) a 3 a 2 a 1 a 0 = (00111001) 2 b 3 b 2 b 1 b 0 s 3 s 2 s 1 s 0 We ignore the “carry out” c 4 generated in the leftmost column Now, find the two’s complement. . . 57 → 0 0 1 1 1 0 0 1 complement → 1 1 0 0 0 1 1 0 add one → 1 1 0 0 0 1 1 1

  5. Addition on binary quantities: example 1 Addition on binary quantities: example 2 0 0 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 0 1 0 1 1 0 1 0 1 1 1 1 0 This shows 3 + 2 = 5 in a 4-bit system This shows 3 + ( − 5) = − 2 in a 4-bit system Addition on binary quantities: example 3 Overflow summary for A + B Outcome A B 0 1 0 0 positive negative correct result 0 1 0 1 negative positive correct result 0 1 0 0 1 0 0 1 negative negative possible overflow This shows 5 + 4 = − 7 in a 4-bit system positive positive possible overflow Oops: arithmetic overflow Informal justification: two’s complement wheel Bit fiddling: arithmetic left shift Bit fiddling: sign extension We use sign extension when we increase the number of bits Various low-level operations on bit strings are often useful For example, we may convert an 4-bit value to a 8-bit value Arithmetic left shift Simply replicate the MSB b 7 b 6 b 5 b 4 b 3 b 2 b 1 b 0 becomes b 6 b 5 b 4 b 3 b 2 b 1 b 0 0 b 3 b 2 b 1 b 0 becomes b 3 b 3 b 3 b 3 b 3 b 2 b 1 b 0 If there is no overflow. . . 0101 00000101 +5 an arithmetic left shift operation computes 2 k , given k 1101 11111101 − 3 n successive arithmetic left shifts computes 2 n k , given k Why does it work?

  6. Bit fiddling: bitwise AND Bit fiddling: bitwise OR — inclusive or a b a AND b a b a OR b 0 0 0 0 0 0 0 1 0 0 1 1 1 0 0 1 0 1 1 1 1 1 1 1 Summary Summary 1 AND b = b 1 OR b = 1 0 AND b = 0 0 OR b = b Bit fiddling: bitwise XOR — exclusive or Bit fiddling: masking operations AND is useful for isolating specific bits a b a XOR b 0 0 0 b 7 b 6 b 5 b 4 b 3 b 2 b 1 b 0 AND 0 0 0 0 0 1 1 1 0 1 1 0 0 0 0 0 b 2 b 1 b 0 1 0 1 1 1 0 OR is useful for inserting ones b 7 b 6 b 5 b 4 b 3 b 2 b 1 b 0 Summary OR 1 1 1 1 1 0 0 0 a = b yields 0 1 1 1 1 1 b 2 b 1 b 0 a � = b yields 1 Bit fiddling: XOR application — testing for equality Floating point representation Use a fixed number of bits, e.g., 32 bits a 7 a 6 a 5 a 4 a 3 a 2 a 1 a 0 Subdivide bits into fields: sign, exponent, fraction XOR b 7 b 6 b 5 b 4 b 3 b 2 b 1 b 0 IEEE floating point standard (including Java’s float ): ? ? ? ? ? ? ? ? 1 sign bit 8 exponent bits, using “excess 127” 23 fraction bits plus “hidden bit” Two bit patterns match if and only if all result bits are 0 Example 1: How can we represent − 6 5 8 as a 32-bit float ? Example 2: What float value is represented by 3D800000 ?

Recommend


More recommend