1 / 42

Topic 4 Computer Mathematics and Logic

Topic 4 Computer Mathematics and Logic. IB Computer Science. A reminder of how we count…. This is how we represent the number 9246.35. We have a 1000s column, a 100s column, etc. All the columns are powers of 10. We count the number of 1000s, 100s, 10s, etc, and sum them up.

radha
Télécharger la présentation

Topic 4 Computer Mathematics and Logic

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Topic 4Computer Mathematics and Logic IB Computer Science

  2. A reminder of how we count… This is how we represent the number 9246.35. We have a 1000s column, a 100s column, etc. All the columns are powers of 10. We count the number of 1000s, 100s, 10s, etc, and sum them up.

  3. You can do the same with powers of 2 Or powers of 3 Or any base you like…

  4. We only count in base 10 because we have ten fingers The Simpsons count in base 8.

  5. In computing We often count in: base 2 (binary) base 8 (octal) base 16 (hexadecimal) You need to know: binary hexadecimal

  6. Counting in other bases • Counting in base 10 • 9 is the biggest number • Then we change columns • Counting in base 2 • 1 is the biggest number • Then we change columns

  7. Counting in other bases • Counting in base 10 • 9 is the biggest number • Then we change columns • Counting in base 2 • 1 is the biggest number • Then we change columns

  8. Counting in hex • Counting in base 16 means that 15 is the biggest number we get to before we change columns • Counting in base 16 presents a problem because we don't have fifteen different digits to make our numbers out of! • So we use letters, as shown… • Don't forget that 10 doesn't mean "ten" if we are counting in hex. • It means "one in the sixteens column and nothing in the units column".

  9. Converting between decimal and binary Convert 45dec to binary • What is the biggest power of 2 in 45? Answer 32, so your first 1 represents 1 x 32, giving 1 • Next, go down the powers of 2 and add on what you need to make 45. • Do we need any 16s? No, because we already have a 32 and 16 would give us 48, so our next number is 0, which represents 0 x 16, giving 10 • Do we need any 8s? Yes, because 32 + 8 is only 40, so our next number is 1, which represents 1 x 8, giving 101 • Do we need any 4s? Yes, because 32 + 8 + 4 is only 44, so our next number is 1, which represents 1 x 4, giving 1011 • Do we need any 2s? No, because adding 2 to our 44 would give us 46, so our next number is 0, which represents 0 x 2, giving 10110 • Do we need any 1s? Yes, because we currently have 44 and we need 1 more to give us 45. So our last number is 1, representing 1 x 1 and giving 101101. • So 45dec in binary is 101101. We will check this result on the next page.

  10. Check 45dec in binary is 101101

  11. Convert the following to binary • 3dec • 7dec • 12dec • 16dec • 20dec • 31dec • 53dec • 64dec • 127dec • 11 • 111 • 1100 • 10000 • 10100 • 11111 • 110101 • 1000000 • 1111111

  12. Convert the following to decimal • 10bin • 101bin • 1101bin • 10010bin • 11001bin • 101010bin • 111111bin • 10000000000bin • 1111111111111bin 2 5 13 18 25 42 63 1024 8191

  13. Converting between decimal and hex Convert 81dec to hex • Hex powers go 1, 16, 256, 4096, etc. They get big quite quickly. • What is the biggest power of 16 in 81dec? Answer 16. • How many sixteens are there in 81dec? Answer 5, with one left over. • So our answer is 51, ie 5 sixteens, plus 1 unit. We will check this result on the next page.

  14. Check 81decin hex is 51

  15. Convert the following to hex • 4dec • 11dec • 14dec • 23dec • 31dec • 45dec • 66dec • 240dec • 301dec 4 B E 17 1F 2C 42 F0 11D

  16. Convert the following to decimal • 5 • C • 10 • 16 • 2A • 32 • FF • 100 • 1AC • 5 • 12 • 16 • 22 • 42 • 50 • 255 • 256 • 428

  17. The good news… • Converting directly between binary and hex is easier than between binary and decimal, or hex and decimal. • You just need to be able to re-create this table in your head.

  18. Convert A4Fhex to binary So 34Fhex = 001101001111bin Notice leading zeroes in answer are not required, but you won't lose marks if you leave them in. Convert 11100010110101bin to hex Notice padding with zeroes enables use of conversion table on previous slide. So 11100010110101bin is 38B5hex

  19. Convert to binary • 1001 • 1101 • 00010011 • 10100001 • 011111001101 • 9 • D • 13 • A1 • 7CD

  20. How many values can n bits represent? • Imagine you have 8 bits to play with • The lowest value is 00000000 • The highest value is 11111111 • That’s 0 up to 255, ie 256 different values • 256 is 28 • In general, n bits can represent 2n different values

  21. Negative numbers • All numbers in the computer are represented in binary, but how are negative numbers represented? (We have no "minus sign" in computer memory!) • Answer: the two’s complement convention: • Everything is as normal, but the most significant bit (MSB) is taken to be negative.

  22. Practice with two’s complement Here we are using 8-bit two’s complement Check you understand: • 00000001 = 20 = 1 • 01000000 = 26= 128 • 01000001 = 26 + 20 = 64 + 1 = 65 • 10000000 = -27= -128 • 10000001 = -27+ 20= -128 + 1 = -127

  23. Range of values • What is the highest value that can be expressed in 8-bit two’s complement? • Well, you want all the positive values, ie 0111111, which is 26 + 25 + 24 + 23 + 22 + 21 + 20 = 127 • What is the lowest value that can be expressed in 8-bit two’s complement? • Well, you want the negative bit, and NONE of the positive bits, ie 10000000, which is -27= -128 • What is the highest value that can be expressed in n-bit two’s complement? • 2n-1-1 • What is the lowest value that can be expressed in n-bit two’s complement? • -2n-1

  24. Range of values

  25. Expressing fractions 1: Fixed-Point Binary • Bits beyond the “bicimal” point are negative powers of 2, ie etc • The bicimal point doesn’t move, hence “fixed point”.

  26. Fixed point binary practice Convert to binary: • 1.5dec • 2.5dec • 4.25dec • 7.625dec Answers: • 1.1 • 10.1 • 100.01 • 111.101 Convert to decimal: • 1.01bin • 101.1bin • 110.011bin • 1000.11bin Answers: • 1.25 • 5.5 • 6.375 • 8.75

  27. Combining Fixed Point and Two's Complement Convert to binary: • -4.75dec Answer: • 1011.01(-8 + 2 + 1 + 0.25) Convert to decimal: • 1001.001bin Answer: • -6.875(-8 + 1 + 0.125) What is the highest value that can be expressed in this format? What is the lowest values? Answers: 111.111 = 7.875 and 1000.000 = -8

  28. Floating Point • Remember scientific notation in maths? • 123.4 becomes 1.234 x 102right? • The same happens in binary. • e.g. 110.1 can be written as 1.101 x 22 • This is called floating point representation, because the decimal point moves. • Test your understanding. Convert to the following to floating point representation: • 10.1 • 1100.1 • 0.00011 • Answers: • 1.01 x 21 • 1.1001 x 23 • 1.1 x 2-4

  29. Storing floating point numbers 1.011 x 23 exponent mantissa • There are two more things to think about: • How do we store the exponent? (We can't store a 3 in binary) • How do we represent negative numbers? • We actually store both the mantissa and the exponent in two's complement form. • Note the position of the bicimal point.

  30. Floating point example: positive mantissa • Positive mantissas are relatively easy: • Convert to decimal the floating-point binary number 010011 0100, if 6 bits are allocated to the mantissa and 4 bits to the exponent. [2 marks] • Remember that bicimal point is after the first bit of the mantissa (ie 0.10011) • Now calculate the exponent. 0100 is (positive) 4, so we shift the bicimal point 4 places to the right, giving 1001.1 • 1001.1 = 8 + 1 + ½ so the final answer is 9.5 • (With a negative exponent, you would just need to shift the bicimal point to the left instead.)

  31. Floating point example: negative mantissa • Recall that we use two's complement because we have no minus sign in the computer's memory. • Well, when you are dealing with a negative mantissa, it is much easier to imagine that you HAVE got a minus sign. • For instance, if you are using 6-bit two's complement, you can consider a mantissa of 1.10000 (-1 + ½ = -½) as -0.10000 (-½) • To easily convert from two's complement to our cheating minus sign format is straightforward: • Flip all the bits • Add 1 to the least significant bit • Put a minus sign in front • To convert back you just do the same thing again and remove the minus sign – it works both ways

  32. Practice complementing What is 10110 (5-bit two's complement) in our cheating minus sign format? • Flip the bits, giving 01001 • Add one to the least significant bit (the right-most) giving: 01010 • Put a minus sign in front: -01010 Check: • 10110 (two's complement) is -1 + 0.25 + 0.125 = -0.675 • -01010 is –(0.5 + 0.125) = -0.675 • Check that the same procedure takes you back to the two's complement format.

  33. Negative mantissa revisited • It's important to know how to complement the mantissa because it makes moving the bicimal point much more straightforward. • Convert to decimal the floating-point binary number 11011 0011, if 6 bits are allocated to the mantissa and 4 bits to the exponent. [2 marks] • First find the cheating format of the mantissa: • 1.1011  0.0100  -0.0101 • (Now it's just like with a positive mantissa a few slides back) • Find the exponent: 0011 = 3 • Move the bicimal point 3 to the right, giving -10.1 • Convert to decimal. Answer -2.5

  34. Converting from decimal to floating point • This is just like converting to scientific notation: • Convert 126.25 to scientific notation. • Move decimal point two to the left 126.25  1.2625 • Add an exponent to compensate 1.2625 x 102 • Convert 5.75 to normalized floating point binary if the mantissa is 6 bits and the exponent is 4 bits: • Convert to fixed point binary: 101.11 • Move bicimal point 3 to the left: 0.10111 (must have a zero outside the bicimal point in case you have a negative mantissa and you need to complement – see next example) • Set the 4-bit exponent to 3 to compensate: 0011 • Full answer: 010111 0011

  35. More examples • Here's how you handle a negative mantissa: • Convert -3.625 to normalized floating point binary if the mantissa is 6 bits and the exponent is 4 bits: • Convert to 6-bit fixed point binary with minus sign: -011.101 • Move bicimal point 2 to the left: -0.11101 • Set the 4-bit exponent to 2 to compensate: 0010 • Complement the mantissa: -0.11101  100010  100011 • Full answer: 100011 0010 • And finally, a negative mantissa and a negative exponent: • Convert to normalized floating point binary if the mantissa is 6 bits and the exponent is 4 bits: • Convert to 6-bit fixed point binary with minus sign: -0.00110 • Move bicimal point 2 to the right: -0.11000 • Set the 4-bit exponent to -2 to compensate: -0010 • Complement the mantissa: -0.00110  111001  111010 • Complement the exponent: -0010  1101  1110 • Full answer: 111010 1110

  36. Important points • Students often find this hard. • There will probably be relatively few marks dedicated to the advanced aspects of this in the exam. • Don't spend a disproportionate amount of time on it. • If you don't like my explanation, try this guy's:http://www.ib-computing.net/html/program/topic_4/floating_point.html

  37. Possible errors • Truncation error: Some numbers require infinite-length mantissas, e.g. one third is 0.0101010101… If you try to store this in a computer then some of the digits get "chopped off" (truncated), with an associated loss of precision. • Overflow error: If you have 3 bits, you can't do the sum 101+ 011 because the answer is 1000, which bigger than the biggest number you can represent. • Underflow error: If you have 4-bit two's complement then the smallest number you can represent is 1000 (or -8). Therefore you can't so the sum -4 - 5, because the answer is 11111 (or -9) which is smaller than the smallest number you can represent. Learn the definitions plus an example for each

  38. Truth tables NAND AND NOT XOR NOR OR

  39. Boolean algebra into words and vice versa • (A) Jessica will go to the party • (B) Fred will go to the party • (C) Chen will be happy • Construct an expression using Boolean algebra for the sentence "Either of Jessica or Fred will go to the party, and Chen will be unhappy." • (A  B)  C • (A xor B) and not C • You need to know the symbols:  AND + OR XOR [overbar] NOT

  40. Logic circuits • You can construct logic circuits out of boolean algebra and vice versa. • This circuit corresponds with the statement on the last slide. It will only produce an output of true if (A xor B) and not C. A B inputs output C • Nand is equivalent to AND followed by NOT (the AND truth table with all the bits flipped) and so has the sense of "anything except both". • Nor is equivalent to OR followed by NOT (the OR truth table with all the bits flipped) and so has the sense of "neither one nor the other".

  41. You should… • Be able to convert between boolean algebra (words or symbols) and logic circuits (maximum of three inputs) • Show that a logic circuit and a boolean algebraic expression are equivalent to each other by comparing their truth tables

  42. (Not A And B) Or (Not(Not A Or (Not A Or B))) (Not A And B) Or (Not(Not A Or Not A Or B)) (Not A And B) Or (Not(Not A Or B)) (Not A And B) Or (A And Not B)) A xor B Karnaugh maps sum of products

More Related