Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic 4 Computer Mathematics and Logic

Similar presentations


Presentation on theme: "Topic 4 Computer Mathematics and Logic"— Presentation transcript:

1 Topic 4 Computer Mathematics and Logic
IB Computer Science

2 A reminder of how we count…
103 102 101 100 10-1 10-2 9 5 9 1000 = 9000 2 100 200 4 10 40 6 1 3 0.1 0.3 5 0.01 0.05 Total This is how we represent the number 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
23 22 21 20 2-1 2-2 Or powers of 3 33 32 31 30 3-1 3-2 Or any base you like… n3 n2 n1 n0 n-1 n-2

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 23 22 21 20 2-1 2-2 1 163 162 161 160 16-1 16-2 F

6 Counting in other bases
Counting in base 10 9 is the biggest number Then we change columns 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 etc 1 2 3 4 5 6 7 8 9 10 11 12 13 etc 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 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 etc 1 2 3 4 5 6 7 8 9 10 11 12 13 etc Counting in base 2 1 is the biggest number Then we change columns

8 Counting in hex 1 2 3 4 5 6 7 8 9 A B C D E F 1 0 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 is only 40, so our next number is 1, which represents 1 x 8, giving 101 Do we need any 4s? Yes, because 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 So 45dec in binary is We will check this result on the next page.

10 Check 45dec in binary is Check: 1 32 = 16 8 4 2 Total 45

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

12 Convert the following to decimal
10bin 101bin 1101bin 10010bin 11001bin 101010bin 111111bin bin bin 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 81dec in hex is 51 Check: 5 16 = 80 1 Total 81

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… Bin Hex 0000 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 8 1001 9 1010 A 1011 B 1100 C 1101 D 1110 E 1111 F 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 3 8 B 5 Convert A4Fhex to binary Convert 11100010110101bin to hex
1 1 1 So 34Fhex = bin Notice leading zeroes in answer are not required, but you won't lose marks if you leave them in. Convert bin to hex 1 Notice padding with zeroes enables use of conversion table on previous slide. 3 8 B 5 So bin is 38B5hex

19 Convert to binary 9 D 13 A1 7CD 1001 1101

20 How many values can n bits represent?
Imagine you have 8 bits to play with The lowest value is The highest value is 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. -28 27 26 25 24 23 22 21 20

22 Practice with two’s complement
Here we are using 8-bit two’s complement Check you understand: = 20 = 1 = 26 = 128 = = = 65 = -27 = -128 = = = -127 -27 26 25 24 23 22 21 20

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 , which is = 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 , 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 3 23 22-1 -22 4 24 23-1 -23 5 25 24-1 -24 6 26 25-1
bits no diff values highest lowest 3 23 22-1 -22 4 24 23-1 -23 5 25 24-1 -24 6 26 25-1 -25 7 27 26-1 -26 8 28 27-1 -27 9 29 28-1 -28 n 2n 2n-1-1 -2n-1

25 Expressing fractions 1: Fixed-Point Binary
23 22 21 20 2-1 2-2 2-3 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 Convert to decimal: 1.01bin 101.1bin bin bin Answers: 1.25 5.5 6.375 8.75

27 Combining Fixed Point and Two's Complement
-23 22 21 20 2-1 2-2 2-3 Convert to binary: -4.75dec Answer: ( ) Convert to decimal: bin Answer: ( ) What is the highest value that can be expressed in this format? What is the lowest values? Answers: = and = -8

28 Floating Point Remember scientific notation in maths?
123.4 becomes x 102 right? The same happens in binary. e.g can be written as 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 Answers: 1.01 x 21 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. -20 2-1 2-2 2-3 2-4 2-5 2-6 2-7 -24 23 22 21 20

30 Floating point example: positive mantissa
Positive mantissas are relatively easy: Convert to decimal the floating-point binary number , 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 ) Now calculate the exponent is (positive) 4, so we shift the bicimal point 4 places to the right, giving = ½ so the final answer is 9.5 (With a negative exponent, you would just need to shift the bicimal point to the left instead.) -20 2-1 2-2 2-3 2-4 2-5 -23 22 21 20 1

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 + ½ = -½) as (-½) 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 (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: Check: 10110 (two's complement) is = is –( ) = 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 , if 6 bits are allocated to the mantissa and 4 bits to the exponent. [2 marks] First find the cheating format of the mantissa:   (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 to scientific notation. Move decimal point two to the left  Add an exponent to compensate 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: Move bicimal point 3 to the left: (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:

35 More examples Here's how you handle a negative mantissa:
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: Move bicimal point 2 to the left: Set the 4-bit exponent to 2 to compensate: 0010 Complement the mantissa:   Full answer: 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: Move bicimal point 2 to the right: Set the 4-bit exponent to -2 to compensate: -0010 Complement the mantissa:   Complement the exponent:  1101  1110 Full answer:

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:

37 Possible errors Learn the definitions plus an example for each
Truncation error: Some numbers require infinite-length mantissas, e.g. one third is … 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 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 , because the answer is (or -9) which is smaller than the smallest number you can represent. Learn the definitions plus an example for each

38 Truth tables A B A nand B 1 AND A B A  B 1 NAND NOT A 1 XOR A B A  B
1 AND A B A  B 1 NAND NOT A 1 XOR A B A  B 1 OR A B A + B 1 NOR A B A nor B 1

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 inputs B 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 B)) (Not A And B) Or (A And Not B)) A xor B Karnaugh maps sum of products


Download ppt "Topic 4 Computer Mathematics and Logic"

Similar presentations


Ads by Google