Exam preparation · A level · OCR H446 1.4.1, AQA 7517 4.4.1.2, Eduqas A500QS 1.8 · about 40 min
Number representation, floating point, Boolean logic, state machines and complexity answered quickly and checked.
[1 mark]What is the denary value of the 8-bit two's complement number 11101100?
[1 mark]A normalised floating point number has mantissa 01101000 (point after the first bit) and exponent 0011. What is its denary value?
[1 mark]Which mantissa is normalised?
[1 mark]In how many rows of its truth table is NOT (A AND B) OR C true?
[1 mark]An algorithm has a loop over n items inside another loop over n items. What is its time complexity?
[1 mark]What does this finite state machine print?
t = {("even", "0"): "even", ("even", "1"): "odd", ("odd", "0"): "odd", ("odd", "1"): "even"}
for text in ["110", "111", ""]:
state = "even"
for symbol in text:
state = t[(state, symbol)]
print(repr(text), state)Write each method as a function, then print the answers.
- twos(bits): bits is a string of 0s and 1s of any length. Return its value as a two's complement integer: the leftmost bit is worth minus its normal place value.
- float_value(mantissa, exponent): both are strings of bits. The mantissa is two's complement with the binary point just after the leftmost bit; the exponent is a two's complement integer. Return the number's value as a float.
- run_fsm(transitions, state, accepting, text): transitions is a dictionary from (state, symbol) tuples to the next state, state is the start state, accepting is a set of accepting states, and text is a string of symbols. Return True if the machine ends in an accepting state.
Then print exactly these six lines, using the functions for the values:
1. 11101100 = <value> using twos;
2. 01101000 x 2^0011 = <value> and 3. 10100000 x 2^1111 = <value> using float_value;
4. 1101: <result> and 5. 1001: <result>, where the result is accepted or rejected, using run_fsm with the even-number-of-1s machine above (states "even" and "odd", start "even", accepting {"even"});
6. true rows: <n>, counting the rows where NOT (A AND B) OR C is true with three nested loops over A, B and C.
The robot stays still.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
def twos(bits):
passPlan your program here, then type it in and press Run.
00011010 with exponent 0101, keeping the value the same.hex_to_denary(text) without using int(text, 16), and test it on 2F and FF.01, and test it with run_fsm.