Python · A Level 12
Binary, Bitwise Operations and Floating Point
Name
Class
Date
Mark / 27
Section A
Circle the letter of the correct answer. One mark each.
- 1What is the range of numbers that can be stored in 8 bit two's complement?1
- A-128 to 127
- B-127 to 127
- C0 to 255
- D-255 to 255
- 2What is the 8 bit two's complement representation of -5?1
- A00000101
- B10000101
- C11111010
- D11111011
- 3What denary number does the 8 bit two's complement pattern 10000000 represent?1
- A-128
- B-1
- C0
- D128
- 4Which bitwise operation would you use to clear a particular bit, leaving the rest unchanged?1
- AXOR with a mask that has a 1 in that position
- BAND with a mask that has a 0 in that position
- COR with a mask that has a 1 in that position
- DNOT, applied to the whole byte
- 5What is
0b1100 ^ 0b0110, written in binary?1- A0100
- B0110
- C1010
- D1110
- 6What is the effect of shifting a binary number left by 2 places?1
- AIt is increased by 2
- BIt is divided by 4
- CIt is multiplied by 2
- DIt is multiplied by 4
- 7What is the hexadecimal value of the binary number 10101100?1
- AAC
- BBC
- CCA
- DA5
- 8In a floating point number, what does adding a bit to the mantissa, and taking one from the exponent, do?1
- AIt increases both the range and the precision
- BIt increases the range but reduces the precision
- CIt does not change either of them
- DIt increases the precision but reduces the range
- 9Why does
0.1 + 0.2 == 0.3give False in Python?1- AA float can only hold whole numbers exactly
- B0.1 cannot be stored exactly in binary, so the results differ by a tiny amount
- CPython has a bug in its addition of decimals
- DThe numbers are stored as text, and text cannot be added
- 10How does UTF-8 relate to ASCII?1
- AIt can only store characters used in English
- BIt replaces all of the ASCII codes with new ones
- CIts first 128 codes are the same as ASCII, and it uses more bytes for other characters
- DIt uses exactly 8 bits for every character, just as ASCII does
Section B
Answer in the space provided. The number of marks tells you how many separate points to make.
- 1WriteWrite the 8 bit two's complement representation of the denary number -45. Show your working.3
- 2CalculateCalculate 25 - 18 in 8 bit binary by using two's complement. Show your working.4
- 3DescribeA byte called settings holds eight on or off options. Bit 0 is the least significant bit. Bit 3 means that sound is on. Describe how bitwise operations can be used to check whether sound is on, and to turn sound off without changing the other seven bits.4
- 4CalculateA floating point number is stored using an 8 bit mantissa and a 4 bit exponent. Both are in two's complement, and the binary point of the mantissa is after the first bit. The mantissa is 01011000 and the exponent is 0011. Calculate the denary value of the number.3
- 5ExplainExplain why the result of adding 0.1 and 0.2 in many programming languages is not exactly 0.3, and how a programmer should compare floating point numbers.3
Mark scheme
Section A
Section B
1 Write the 8 bit two's complement representation of the denary number -45. Show your working. 3
One mark for each of the following, to a maximum of 3.
- 45 in binary is 00101101
- The bits are inverted to give 11010010, and 1 is added
- The answer is 11010011
Model answer. 45 in 8 bit binary is 00101101. To make it negative, invert every bit, giving 11010010, and add 1, which gives 11010011. This is -128 + 64 + 16 + 2 + 1, which is -45, so the answer is 11010011.
2 Calculate 25 - 18 in 8 bit binary by using two's complement. Show your working. 4
One mark for each of the following, to a maximum of 4.
- 25 is 00011001 and 18 is 00010010
- -18 is found by inverting 00010010 to 11101101 and adding 1, giving 11101110
- The two are added: 00011001 plus 11101110
- The carry out of the 8th bit is ignored, giving 00000111, which is 7
Model answer. 25 is 00011001 and 18 is 00010010. To get -18, invert 00010010 to give 11101101 and add 1 to give 11101110. Adding 00011001 and 11101110 gives 1 00000111, and the carry out of the eighth bit is ignored, so the result is 00000111, which is 7.
3 A byte called settings holds eight on or off options. Bit 0 is the least significant bit. Bit 3 means that sound is on. Describe how bitwise operations can be used to check whether sound is on, and to turn sound off without changing the other seven bits. 4
One mark for each of the following, to a maximum of 4.
- A mask with only bit 3 set, 00001000, is used
- To check, the byte is ANDed with the mask and the result is not zero if sound is on
- To turn it off, the byte is ANDed with the inverse of the mask, 11110111
- This sets bit 3 to 0 and leaves the other bits as they were
Model answer. A mask is made with only bit 3 set, 00001000. To check whether sound is on, the byte is ANDed with the mask, and if the result is not zero then bit 3 is 1 and sound is on. To turn sound off, the byte is ANDed with the inverse of the mask, 11110111. That sets bit 3 to 0, and since every other bit is ANDed with 1, the other seven bits remain as they were.
4 A floating point number is stored using an 8 bit mantissa and a 4 bit exponent. Both are in two's complement, and the binary point of the mantissa is after the first bit. The mantissa is 01011000 and the exponent is 0011. Calculate the denary value of the number. 3
One mark for each of the following, to a maximum of 3.
- The mantissa is 0.1011000 in binary, which is 0.6875
- The exponent is 3
- The value is 0.6875 times 8, which is 5.5
Model answer. The mantissa 01011000 with the point after the first bit is 0.1011000, which is a half plus an eighth plus a sixteenth, 0.6875. The exponent 0011 is 3, so the mantissa is multiplied by 2 to the power of 3, which is 8. The value is 0.6875 times 8, which is 5.5.
5 Explain why the result of adding 0.1 and 0.2 in many programming languages is not exactly 0.3, and how a programmer should compare floating point numbers. 3
One mark for each of the following, to a maximum of 3.
- Some decimal fractions, such as 0.1, cannot be stored exactly in binary
- The stored values are rounded, so a small error occurs in the result
- So two values should be compared by checking that the difference is smaller than a small tolerance, not for equality
Model answer. Decimal fractions such as 0.1 and 0.2 are recurring fractions in binary, so they cannot be stored exactly in a mantissa with a limited number of bits, and each is stored as a rounded approximation. The small errors mean that the sum is a tiny amount away from 0.3. A programmer should therefore compare floating point values by checking that the difference between them is smaller than a small tolerance, rather than testing for equality.