A4 Python for engineers · Practice
Bits, bytes and binary data
Problems dealt one at a time, with new numbers every time. The kinds you miss come back more often, and you can keep going as long as you like.
Check
Write field(x, pos, width) returning the width-bit field of x that starts at bit pos. Then set f = field(reg, pos, width) with reg = 0xBEEF, pos = 0 and width = 4.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write set_field(x, pos, width, value) returning x with its width-bit field at pos replaced by value, leaving every other bit alone. Then set y = set_field(reg, pos, 4, v) with reg = 0xBEEF, pos = 0 and v = 9.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write tmp102(b1, b2) that turns a TMP102's two bytes (most significant first) into °C: the top 12 bits of the 16-bit value, two's complement, times 0.0625. Then set t for the byte pair the starter picks.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
What is 0x66 in decimal?
Check
Write checksum(bs) returning the XOR of all the bytes in a list, and valid(packet) returning True when the last byte of packet equals the checksum of the bytes before it. Then set c = checksum(data) and ok = valid(packet) for the packet the starter picks.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A 16-bit signed register reads 32767 when you treat it as unsigned. What signed value does it hold?
Check
Set ones to how many bits of x = 0x00FF are 1, using a loop with & 1 and >>.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Set s to the byte b = 91 written as exactly 8 binary digits (with leading zeros) and h as exactly two capital hex digits, both as strings.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
A kind of problem counts as solid once you've got it right on the first try twice in a row. Back to the lesson · Practice every topic in A4