Topic 4Computer Mathematics and LogicIB Computer Science
A reminder of how we count…
103 102 101 100 10-1 10-2
9 2 4 6 3 5
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.
9 1000 = 9000
2 100 = 200
4 10 = 40
6 1 = 6
3 0.1 = 0.3
5 0.01 = 0.05
Total = 9346.35
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
We only count in base 10 because we have ten fingers
The Simpsons count in base 8.
In computingWe 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 1 1 1 0 0
163 162 161 160 16-1 16-2
0 0 0 F 0 0
Counting in other bases• Counting in base 10• 9 is the biggest number• Then we change columns
0123456789
10111213etc
01
1011
100101110111
100010011010101111001101
etc
• Counting in base 2• 1 is the biggest number• Then we change columns
Counting in other bases• Counting in base 10• 9 is the biggest number• Then we change columns
0123456789
10111213etc
01
1011
100101110111
100010011010101111001101
etc
• Counting in base 2• 1 is the biggest number• Then we change columns
Counting in hex 0123456789ABCDEF
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".
Converting between decimal and binaryConvert 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.
Check
Check:1 32 = 320 16 = 01 8 = 81 4 = 40 2 = 01 1 = 1Total = 45
45dec in binary is 101101
Convert the following to binary
• 3dec
• 7dec
• 12dec
• 16dec
• 20dec
• 31dec
• 53dec
• 64dec
• 127dec
• 11• 111• 1100• 10000• 10100• 11111• 110101• 1000000• 1111111
Convert the following to decimal
• 10bin
• 101bin
• 1101bin
• 10010bin
• 11001bin
• 101010bin
• 111111bin
• 10000000000bin
• 1111111111111bin
25
1318254263
10248191
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.
Check
Check:5 16 = 801 1 = 1Total = 81
81dec in hex is 51
Convert the following to hex
• 4dec
• 11dec
• 14dec
• 23dec
• 31dec
• 45dec
• 66dec
• 240dec
• 301dec
4BE
171F2C42F0
11D
Convert the following to decimal
• 5• C• 10• 16• 2A• 32• FF• 100• 1AC
• 5• 12• 16• 22• 42• 50• 255• 256• 428
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.
Bin Hex0000 0
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
0 0 1 1 1 0 0 0 1 0 1 1 0 1 0 1
0 0 1 1 0 1 0 0 1 1 1 1
3 4 F
So 34Fhex = 001101001111bin
3 8 B 5
Convert A4Fhex to binary
Convert 11100010110101bin to hex
So 11100010110101bin is 38B5hex
Notice padding with zeroes enables use of conversion table on previous slide.
Notice leading zeroes in answer are not required, but you won't lose marks if you leave them in.
Convert to binary• 9• D• 13• A1• 7CD
• 1001• 1101
• 00010011• 10100001
• 011111001101
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
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
Practice with two’s complementHere 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
-27 26 25 24 23 22 21 20
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
Range of valuesbits 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
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”.
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
Combining Fixed Point and Two's Complement
Convert to binary:• -4.75dec
-23 22 21 20 2-1 2-2 2-3
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
Floating Point• Remember scientific notation in maths?• 123.4 becomes 1.234 x 102
right?
• 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
Storing floating point numbers
• 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.
1.011 x 23mantissa exponent
-20 2-1 2-2 2-3 2-4 2-5 2-6 2-7 -24 23 22 21 20
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.)
-20 2-1 2-2 2-3 2-4 2-5 -23 22 21 20
0 1 0 0 1 1 0 1 0 0
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:1. Flip all the bits2. Add 1 to the least significant bit3. 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
Practice complementing
What is 10110 (5-bit two's complement) in our cheating minus sign format?1. Flip the bits, giving 010012. Add one to the least significant bit (the right-most) giving:
010103. 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.
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
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
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
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
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
Truth tables
A B A nand B
0 0 1
0 1 1
1 0 1
1 1 0
A B A + B
0 0 0
0 1 1
1 0 1
1 1 1
A A
0 1
1 0
A B A B
0 0 0
0 1 1
1 0 1
1 1 0
NAND
ORXOR
NOT
A B A nor B
0 0 1
0 1 0
1 0 0
1 1 0
NOR
A B A B
0 0 0
0 1 0
1 0 0
1 1 1
AND
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
Logic circuits
• You can construct logic circuits out of boolean algebra and vice versa.
A
B
Coutputin
puts
• 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.
• 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".
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
(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 mapssum of products