================================================= Assignment #04 - Integer Numeric Conversions ================================================= - Ian! D. Allen - idallen@idallen.ca - www.idallen.com Answers will be posted shortly after the due date/time and discussed in class, if there are any questions about the answers. Late assignments may or may not be marked. Submission method: Create a plain text file using the exact name "assignment04.txt" Upload the file via the Assignment04 "Upload File" facility in Blackboard. Use "Attach File" and "Submit" to upload your plain text file. No wordprocessor documents. Do not send email. Use only "Attach File". Use the file name given above. Upload only one single file of plain text, not HTML, not MSWord. No fonts, no word-processing. Plain text only. Did I mention that the format is plain text (VIM/Nano/Pico/Gedit or Notepad)? NO WORD PROCESSOR DOCUMENTS ACCEPTED. No marks are awarded for submitting under the wrong assignment number. Not all assignments will be marked. See the Week 1 Notes for details. Answers will be posted after the due date/time so that you can check your answers before coming to class and ask questions about the answers in class. Please check your answers (and my answers!). I go over each assignment in class if there are questions about the answers. No questions means no review - I'll presume you know the material. Questions similar to ones on these assignments will appear on your tests and exams. DO THIS: Edit this file and answer the following questions underneath each question, showing the method or formula you used to get the answer. Many of the questions already give you the answer - you must show the full method you use to generate that answer. Upload the file containing the question, methods, formulas, and answers before the due date. Some of the answers below will require reading the links published in the weekly course notes. Full marks are awarded only if you show your method, the same method you will have to use on your midterm test (no calculators). ============================================================================== Full marks are awarded only if you show your method. Take care to panic and do bit-flipping only for *negative* numbers. 1. Convert binary 11101101001111001001000000011010(2) to hexadecimal (base 16) by using groups of four bits, starting from the right. Hint: Your answer will have eight hex digits and the rightmost hex digit will be "A": (See ECOA2e Chapter 2 slide 27 for the method.) 0 = 0000 1 = 0001 2 = 0010 3 = 0011 4 = 0100 5 = 0101 1110 1101 0011 1100 1001 0000 0001 1010 (from right) 6 = 0110 E D 3 C 9 0 1 A 7 = 0111 8 = 1000 9 = 1001 A = 1010 B = 1011 C = 1100 D = 1101 E = 1110 F = 1111 = ED3C901A(16) 2. Convert binary 110011101010001100111000(2) to octal (base 8) by using groups of three bits, starting from the right. Hint: Your answer will have eight octal digits and the rightmost octal digit will be "0": (See ECOA2e Chapter 2 slide 27 for the method.) 0 = 000 1 = 001 2 = 010 3 = 011 110 011 101 010 001 100 111 000 (group from right) 4 = 100 6 3 5 2 1 4 7 0 5 = 101 6 = 110 7 = 111 = 63521470(8) 3. Write 11101110(2) in octal (base 8) and hexadecimal (base 16). 11101110(2) = 011 101 110(2) (group by three bits from right) 3 5 6(8) = 356(8) = 1110 1110(2) (group by four bits from right) E E(16) = EE(16) 4. Write 00011011(2) in octal (base 8) and hexadecimal (base 16). 00011011(2) = 000 011 011(2) (group by three bits from right) 0 3 3(8) = 033(8) = 0001 1011(2) (group by four bits from right) 1 B(16) = 1B(16) 5. Write 11110101(2) in octal (base 8) and hexadecimal (base 16). 11110101(2) = 011 110 101(2) (group by three bits from right) 3 6 5(8) = 365(8) = 1111 0101(2) (group by four bits from right) F 5(16) = F5(16) 6. Write 10101001(2) as octal (base 8) and hexadecimal (base 16). 10101001(2) = 010 101 001(2) (group by three bits from right) 2 5 1(8) = 251(8) = 1010 1001(2) (group by four bits from right) A 9(16) = A9(16) 7. Write 10010010(2) as octal (base 8) and hexadecimal (base 16). 10010010(2) = 010 010 010(2) (group by three bits from right) 2 2 2(8) = 222(8) = 1001 0010(2) (group by four bits from right) 9 2(16) = 92(16) 8. Convert 6253(8) octal to CAB(16) hexadecimal by regrouping binary bits. 6253(8) = 110 010 101 011(2) = 1100 1010 1011(2) = CABh 6 2 5 3(8) = C A B(16) 9. Convert 8A9Dh (hexadecimal) to octal (base 8) by regrouping binary bits. Convert 8A9D(16) to binary and re-group in 3-bit chunks for octal: 8 A 9 D(16) 1000 1010 1001 1101(2) 001 000 101 010 011 101(2) (always group from the right) 1 0 5 2 3 5(8) 10. Convert ABCEh (hexadecimal) to octal (base 8) by regrouping binary bits. Convert ABCE(16) to binary and re-group in 3s: A B C E(16) 1010 1011 1100 1110(2) 001 010 101 111 001 110(2) (always group from the right) 1 2 5 7 1 6(8) 11. Convert 8-bit 10010010(2) binary unsigned to 146(10) decimal. (See ECOA2e Chapter 2 slide 8 for the method.) 10010010(2) = ____________________(10) = 0 x 2**0 + 1 x 2**1 + 0 x 2**2 + 0 x 2**3 + 1 x 2**4 + 0 x 2**5 + 0 x 2**6 + 1 x 2**7 = 146(10) Or in hex 10010010(2) = 1001 0010(2) = 92(16) = 9*16 + 2 = 146(10) 12. Write 23(10) in octal. (See ECOA2e Chapter 2 slides 11-17 for two methods.) Subtract powers of eight to convert decimal to octal. Powers: 8**0 = 1, 8**1 = 8, 8**2 = 64, 8**3 = (2**3)**3 = 2**9 = 512 23 -16 = 2 x 8**1 --- 7 - 7 = 7 x 8**0 --- 0 = 27(8) 13. Convert 16-bit unsigned hexadecimal 0x142B to 5,163(10) decimal. Write down some hex digits: A=10, B=11, C=12, D=13, E=14, F=15 Write down some powers of 16: 16**0=1, 16**1=16, 16**2=256, 16**3=4096 Now add up the given powers of 16 in 142B(16): B times 16**0 = 11 * 1 = 11 2 times 16**1 = 2 * 16 = 32 4 times 16**2 = 4 * 256 = 1024 1 times 16**3 = 1 * 4096 = 4096 TOTAL = 5163(10) decimal 14. Convert unsigned 16-bit 1AAC(16) to 6,828(10) decimal. 1AAC(16) = 12 X 16**0 + 10 X 16**1 + 10 X 16**2 + 1 X 16**3 = 6828(10) 15. Convert 16-bit unsigned FFFFh to decimal 65,535 using a trick to reduce the number of multiplications and additions. FFFF(16) is hard - lots of multiplying and adding. FFFF + 1 = 10000(16) is easy (one multiply), and is just one bigger than FFFF(16). 10000(16) = 1 x 16**4 = (2**4)**4 = 2**16 = 65,536 (a famous number) so our answer FFFF(16) must be one less than that: 65,535 16. Convert unsigned 16-bit 7FFE(16) to decimal 32,766(10). Hint: Use a trick to cut down on the number of multiplies and adds. 7FFE(16) First do it the hard way: = 14 X 16**0 + 15 X 16**1 + 15 X 16**2 + 7 X 16**3 = 32766(10) The trick is to add two before converting, then subtract it after: 7FFE(16) + 2 = 8000(16) = 8 * 16**3 = 2^3 * 2^12 = 2^15 = 32,768 so our answer 7FFFE(16) must be two less than that: 32,766 17. Convert 16-bit unsigned 8001h to decimal 32,769. 8001(16) = 1 x 16**0 + 0 x 16**1 + 0 x 16**2 + 8 x 16**3 = 1 + 8 x 4096 = 1 + 2^3 * 2^12 = 1 + 2^15 = 32769 18. Convert 16-bit unsigned A1A3h to decimal 41,379. A1A3(16) = 3 X 16**0 + 10 X 16**1 + 1 X 16**2 + 10 X 16**3 = 41379(10) 19. Convert binary fraction 0.1111(2) to 0.9375 decimal. 0.1111(2) = 1 * 2**(-1) + 1 * 2**(-2) + 1 * 2**(-3) + 1 * 2**(-4) = 0.5 + 0.25 + 0.125 + 0.0625 = 0.9375(10) 20. Convert 25(10) decimal to 11001(2) binary. (See ECOA2e Chapter 2 slides 11-17 for two methods.) Subtraction method: take 1 x 16 from 25 leaving 9 <-- leftmost bit of answer is here take 1 x 8 from 9 leaving 1 take 0 x 4 from 1 leaving 1 take 0 x 2 from 1 leaving 1 take 1 x 1 from 1 leaving 0 <-- rightmost bit of answer is here OR: Division method: 25 div by 2 is 12 rem 1 <-- rightmost bit of answer 12 div by 2 is 6 rem 0 6 div by 2 is 3 rem 0 3 div by 2 is 1 rem 1 1 div by 2 is 0 rem 1 <-- leftmost bit of answer 21. Convert 39(10) to 27 hexadecimal. (See ECOA2e Chapter 2 slides 11-17 for two methods.) Subtract powers of sixteen to convert decimal to hexadecimal: Powers: 16^0 = 1, 16^2 = 256, 16^3 = (2^4)^3 = 2^12 = 4096 39 -32 = 2 x 16**1 --- 7 - 7 = 7 x 16**0 --- 0 = 27(16) 22. Convert 155(10) decimal to 10011011(2) binary. Subtraction method: take 1 x 128 from 155 leaving 27 <-- leftmost bit of answer is here take 0 x 64 from 27 leaving 27 take 0 x 32 from 27 leaving 27 take 1 x 16 from 27 leaving 11 take 1 x 8 from 11 leaving 3 take 0 x 4 from 3 leaving 3 take 1 x 2 from 3 leaving 1 take 1 x 1 from 1 leaving 0 <-- rightmost bit of answer is here OR Division method: 155 div by 2 is 77 rem 1 <-- rightmost bit of answer 77 div by 2 is 38 rem 1 38 div by 2 is 19 rem 0 19 div by 2 is 9 rem 1 9 div by 2 is 4 rem 1 4 div by 2 is 2 rem 0 2 div by 2 is 1 rem 0 1 div by 2 is 0 rem 1 <-- leftmost bit of answer 23. Convert 0.75(10) to 0.11(2) binary (see ECOA2e Chapter 2 slide 19). Subtraction method: take 1 x 0.5 from 0.75 leaving 0.25 <-- leftmost bit of answer is here take 1 x 0.25 from 0.25 leaving 0 <-- rightmost bit of answer is here OR Multiplication method: 0.75 times 2 is 1 plus 0.5 <-- leftmost bit of answer is here 0.5 times 2 is 1 plus 0.0 <-- rightmost bit of answer is here 24. Convert 0.8125(10) to 0.1101(2) binary (see ECOA2e Chapter 2 slide 21). Subtraction method: take 1 x 0.5 from 0.8125 leaving 0.3125 <-- leftmost bit take 1 x 0.25 from 0.3125 leaving 0.0625 take 0 x 0.125 from 0.0625 leaving 0.0625 take 1 x 0.0625 from 0.0625 leaving 0.0 <-- rightmost bit OR Multiplication method: 0.8125 times 2 is 1 plus 0.625 <-- leftmost bit of answer is here 0.625 times 2 is 1 plus 0.25 0.25 times 2 is 0 plus 0.5 0.5 times 2 is 1 plus 0 <-- rightmost bit of answer is here 25. Convert 0.34350(10) to 0.0101(2) binary, stopping at four fractional bits. (This does not mean truncating 0.34350 to 0.3435. It means calculating the binary fraction for 0.34350 and stopping when you have four binary digits to the right of the binary radix point, as shown in the answer above. The remaining bits are left uncalculated, and so the answer is actually wrong - it doesn't add up. Computers are like that - sometimes you don't have space for all the binary bits and the answer is wrong.) (see ECOA2e Chapter 2 slides 18-24). This is the subtraction method, subtracting negative powers of two: 0.34350 -0.00000 = 2**-1 x 0 <-- leftmost bit -------- 0.34350 -0.25000 = 2**-2 x 1 -------- 0.09350 -0.00000 = 2**-3 x 0 -------- 0.09350 -0.06250 = 2**-4 x 1 <-- rightmost bit - stop at 4 bits -------- 0.03100 ... we could keep going ... Answer (to only four fractional bits): 0.0101(2) 26. Assuming an 8-bit word, show using unsigned binary how such a word would be encoded to represent the decimal number 86: * METHOD 1 - Successive Subtraction of powers of two Write down some powers of two: ... 256 128 64 32 16 8 4 2 1 Biggest power of two that fits 86: 64 [= 2**6] 86 - 64 = 22 Biggest power of two that fits 22: 16 [= 2**4] 22 - 16 = 6 Biggest power of two that fits 6: 4 [= 2**2] 6 - 4 = 2 Biggest power of two that fits 2: 2 [= 2**1] 2 - 2 = 0 Nothing left --> we're done. Write down *all* the powers of two: 86 = 1*64 + 0*32 + 1*16 + 0*8 + 1*4 + 1*2 + 0*1 Express the powers of two that we used in binary: 2^6 2^5 2^4 2^3 2^2 2^1 2^0 <-- all powers of two 64 32 16 8 4 2 1 <-- in decimal -- -- -- -- -- -- -- 1 0 1 0 1 1 0 = 1010110(2) in binary * METHOD 2 - Successive division by two 86 divided by 2 is 43 remainder 0 <-- rightmost bit 43 divided by 2 is 21 remainder 1 21 divided by 2 is 10 remainder 1 10 divided by 2 is 5 remainder 0 5 divided by 2 is 2 remainder 1 2 divided by 2 is 1 remainder 0 1 divided by 2 is 0 remainder 1 <-- leftmost bit Answer (write the remainders right-to-left): 1010110(2) in binary 27. Convert decimal 2.5625 to binary. Write down some powers of 2: 2**1=2, 2**0=1, 2**(-1)=0.5, 2**(-2)=0.25, 2**(-3)=0.125, 2**(-4)=0.0625 2.0 in binary is simply 10.0(2) What (negative) powers of two sum to make 0.5625? Biggest negative power of two that fits 0.5625 is 2**(-1) = 0.5 Subtract that: 0.5625 - 0.5 = 0.0625 Biggest power of two that fits 0.0625 is 2**(-4) = 0.0625 Subtract that: 0.0625 - 0.0625 = 0 --> we're done. Express the (negative) powers of two that we used in binary: 2^-1 2^-2 2^-3 2^-4 <-- negative powers of two 0.5 0.25 0.125 0.0625 <-- in decimal --- ---- ----- ------ 0 . 1 0 0 1 = 0.1001(2) in binary Therefore 2.5625(10) decimal is 10.0(2) + 0.1001(2) = 10.1001(2) in binary. 28. Show that Christmas == Halloween by converting DEC 25 to OCT 31 (convert DECimal 25 into OCTal 31) 25(10) [Christmas is December 25] -24 = 3 x 8**1 --- 1 - 1 = 1 x 8**0 --- 0 = 31(8) [Halloween is October 31] 29. Convert 23 to 8-bit 00010111 binary one's complement. Number 23 is positive. Just treat as unsigned binary. Convert to binary by subtracting powers of two: (+)23 -16 = 1 x 2**4 --- 7 - 0 = 0 x 2**3 --- 7 - 4 = 1 x 2**2 --- 3 - 2 = 1 x 2**1 --- 1 - 1 = 1 x 2**0 --- 0 = 0001 0111 [one's complement] 30. Convert -23 to 8-bit 11101000 binary one's complement. Number -23 is negative. Do the positive, then negate it: = 0001 0111 [unsigned 23 from above] = 1110 1000 [flip bits to get one's complement] 31. Convert 23 to 8-bit 00010111 binary two's complement. Number is positive. Just treat as unsigned binary (above): = 0001 0111 [unsigned 23 from above] 32. Convert -23 to 8-bit 11101001 binary two's complement. Number is negative. Treat as positive, do one's complement (above), then add one: = 0001 0111 [unsigned 23 from above] = 1110 1000 [flip bits for one's complement] = 1110 1001 [add one for two's complement -23] 33. Convert -10 to 8-bit 11110101 binary one's complement. Number -10 is negative. Do the positive, then negate (flip) it: (+)10 - 8 = 1 x 2**3 --- 2 - 2 = 1 x 2**2 --- 1 - 0 = 0 x 2**1 --- 1 - 0 = 0 x 2**0 --- 0 = 0000 1010 [unsigned 10] = 1111 0101 [flip bits to get one's complement -10] 34. Convert -10 to 8-bit 11110110 binary two's complement. Number -10 is negative. Treat as positive, then do one's complement (above), then add one: = 0000 1010 [unsigned 10 from above] = 1111 0101 [flip bits for one's complement] = 1111 0110 [add one for two's complement -10] 35. Convert 8-bit 10010011 binary sign-magnitude to -19 decimal (note the negative). Binary number 10010011 is negative (sign bit on). Make it positive, convert to decimal, put a minus sign in front of it: 10010011 [sign-magnitude] 00010011 [remove sign bit to make it unsigned, then convert] = 1 x 2**0 + 1 x 2**1 + 0 x 2**2 + 0 x 2**3 + 1 x 2**4 + 0 x 2**5 + 0 x 2**6 = 19 --> (put back the minus sign) -19 36. Convert 8-bit 10010011 binary one's complement to -108 decimal (note the negative). Binary number 10010011 is negative (sign bit on). Make it positive, convert to decimal, put a minus sign in front of it: 10010011 [one's complement negative number] 01101100 [flip bits to make it unsigned binary, then convert] = 0 x 2**0 + 0 x 2**1 + 1 x 2**2 + 1 x 2**3 + 0 x 2**4 + 1 x 2**5 + 1 x 2**6 = 108 --> (put back the minus sign) -108 37. Convert 8-bit 10010011 binary two's complement to -109 decimal (note the negative). Binary number 10010011 is negative (sign bit on). Make it positive, convert to decimal, put a minus sign in front of it: 10010011 [two's complement negative number] 01101100 [flip bits and then ...] 01101101 [... add one - number is now positive] = 1 x 2**0 + 0 x 2**1 + 1 x 2**2 + 1 x 2**3 + 0 x 2**4 + 1 x 2**5 + 1 x 2**6 = 109 --> (put back the minus sign) -109 38. Convert 16-bit two's complement 8000h to decimal -32,768 (note the negative). Number 8000h is negative (sign bit on). Make it positive, convert to decimal, put a minus sign in front of it: 8000h [two's complement negative number] 7FFFh [flip bits, using hex bit flip table, and then ...] 8000h [... add one - number is now positive] Add up one power of 16 (using some exponent math to simplify): 8 * 16**3 = 2**3 * (2**4)**3 = 2**3 * 2**12 = 2**15 = 32,768 (famous number) Put back the minus sign: -32,768 39. Convert 16-bit two's complement A123h to decimal -24,285 (note negative). Number A123h is negative (sign bit on). Make it positive, convert to decimal, put a minus sign in front of it: A123h [two's complement] 5EDCh [flip bits, using hex bit flip table, and then ...] 5EDDh [... add one - number is now positive] Add up four powers of 16, from 16**0 up to 16**3: 5 * 16**3 + E * 16**2 + D * 16**1 + D * 16**0 = 5 * 4096 + 14 * 256 + 13 * 16 + 13 * 1 = 24,285 Put back the minus sign: -24,285 40. Convert 16-bit two's complement FFFFh to decimal -1 (note the negative). Number FFFFh is negative (sign bit on). Make it positive, convert to decimal, put a minus sign in front of it (use the method above): FFFFh -> [flip bits] 0000h -> [add one] 0001h = 1 -> [add sign] -1 41. Here are 16-bit two's complement numbers. Put minus signs in front of all the negative numbers. 9000h A001h 7FC5h C123h B3FFh The sign bit is on for hex digits 8 (1000) through F (1111). All the numbers above except 7FC5h are negative. 42. Indicate using an "X" which of the following positive values will *NOT* fit correctly in a 32-bit two's complement integer: (2**29)-1 2**29 (2**29)+1 (2**30)-1 2**30 (2**30)+1 (2**31)-1 2**31 (2**31)+1 (2**32)-1 2**32 (2**32)+1 (2**33)-1 2**33 (2**33)+1 (2**30)+(2**29) (2**30)+(2**29)+(2**3)+(2**2)+(2**1) The range of 32-bit two's complement integers is (from the formula): Largest = + ( (2**(N-1)) - 1 ) = + (2**31 - 1) [ = +(2 Gi) - 1 ] Smallest = - ( (2**(N-1)) ) = - (2**31) [ = -2 Gi ] For 32-bit two's complement integers, anything bigger than (2**31 - 1) won't fit, so mark those with "X": (2**29)-1 2**29 (2**29)+1 (2**30)-1 2**30 (2**30)+1 (2**31)-1 X2**31 X(2**31)+1 X(2**32)-1 X2**32 X(2**32)+1 X(2**33)-1 X2**33 X(2**33)+1 (2**30)+(2**29) (2**30)+(2**29)+(2**3)+(2**2)+(2**1) If possible, convert the following decimal values into 2's complement form, assuming a 12-bit word. Show your results in both binary and hexadecimal. 43. -1 decimal to 12-bit two's complement binary and hex. -1 is negative, so treat as positive and bit flip later 1(10) = 001h as 12-bits hex --> FFEh (flip the bits using bit-flip table) --> FFFh (add one for two's complement) FFFh = 1111 1111 1111(2) (or - just remember that -1 is always "all bits on") 44. +693 decimal to 12-bit two's complement binary and hex. 693(10) = ??? hex -> use powers-of-16 table: 1, 16, 256, 4096 693 / 256 = 2 rem 181 181 / 16 = 11 rem 5 [ and we write 11(10) = B(16) ] 5 / 1 = 5 rem 0 +693 = 2B5h = 0010 1011 0101(2) 45. -693 decimal to 12-bit two's complement binary and hex. -693 is negative so treat as positive and bit flip later +693(10) = 2B5h (from above) --> D4Ah (flip the bits using bit-flip table) --> D4Bh (add one for two's complement) -693 = D4Bh = 1101 0100 1011(2) 46. +2048 decimal to 12-bit two's complement binary and hex. 2048(10) = ??? hex -> use powers-of-16 table: 1, 16, 256, 4096 2048 / 256 = 8 rem 0 0 / 16 = 0 rem 0 0 / 1 = 0 rem 0 = 800h which is negative in 12 bits! --> 2048 is too big to fit in 12 bits two's complement --> max (using formula) is +((2**11)-1) = +2047 --> too big! 47. -2048 decimal to 12-bit two's complement binary and hex. -2048 is negative so treat as positive and bit flip later +2048(10) = 800h (from above) --> 7FFh (flip the bits using bit-flip table) --> 800h (add one for two's complement) -2048 = 800h = 1000 0000 0000(2) (or - just remember that the most negative number has the sign bit on and nothing else in two's complement) 48. +4097 decimal to 12-bit two's complement binary and hex. --> doesn't fit in 12 bits (use the formula to know this) Full marks are awarded only if you show your method. -- | Ian! D. Allen - idallen@idallen.ca - Ottawa, Ontario, Canada | Home Page: http://idallen.com/ Contact Improv: http://contactimprov.ca/ | College professor (Free/Libre GNU+Linux) at: http://teaching.idallen.com/ | Defend digital freedom: http://eff.org/ and have fun: http://fools.ca/