Decide how the bits will be interpreted
The same bit pattern can mean different things. Eight 1 bits represent 255 as an unsigned byte and −1 as an eight-bit two’s complement integer. A character encoding interprets a group of bits in a different way again. Whenever a question gives a bit pattern, identify its width and representation before interpreting it. Do not infer a negative number just because the leftmost bit is 1. That bit is worth positive 128 in an unsigned byte and negative 128 in eight-bit two’s complement.
| Pattern | Unsigned | Eight-bit two’s complement |
|---|---|---|
00000000 | 0 | 0 |
01111111 | 127 | 127 |
10000000 | 128 | −128 |
11111111 | 255 | −1 |
One decoding method for signed bytes is to calculate the unsigned number and subtract 256 when the first bit is 1. For 11111110, the unsigned value is 254. Subtracting 256 gives −2. This works because the leftmost contribution changes from +128 to −128, a difference of 256. The signed range is −128 to 127. Positive and negative limits are different because one pattern is used for zero.
Connect a trace to place value
Start an accumulator at zero. On reading a new bit, double the value already accumulated, then add the new bit. Doubling shifts each existing place value left by one. For 0101, the totals are 0, 1, 2, 5. Leading zeros matter when the task asks for exactly eight bits even though they do not change an unsigned numeric value. Keep the raw input as text for the length check. Converting early could lose those zeros or accept text that the format rejects.
Binary addition uses a carry when the sum in a column reaches 2. 1 + 1 gives binary 10. For 11111111 + 00000001, the carry travels through all eight positions and produces 100000000. This is 256. In a fixed-width eight-bit unsigned representation, that sum does not fit. In this exercise we report overflow explicitly. Python's normal integer operation still produces the mathematical answer because its integers are not restricted to eight bits.
Read characters, codes and bytes as separate things
The string "65" is made of two characters. It differs from the integer 65 and from the single character "A". ord("A") gives the Unicode code point 65, which agrees with ASCII for A. "A".encode("utf-8") produces one byte, while "£".encode("utf-8") uses two bytes. When a file task asks for storage size, count encoded bytes or use the stated encoding assumptions. Do not treat every character as one byte. An eight-bit quantity is a byte regardless of whether it represents a number, part of a letter or part of an instruction.
Practise a Paper 2 change request
Write a helper decode_signed(bits) that returns the signed value of a valid eight-bit pattern. Write a caller that checks the length and rejects any character outside 0 and 1, then prints the result or Invalid. Before running, plan tests for −128, −1, zero, +127, a seven-character pattern and an invalid digit. Explain why a test using only values that start with 0 cannot detect a bug that decodes everything as unsigned. Use Run with my input in a lab and supply one input line for this separate exercise.
Group bits to read hexadecimal
Each hexadecimal digit represents four bits. Split 10111110 into 1011 and 1110. Their values are 11 and 14, written B and E, so the byte is BE in hexadecimal. This is a shorter notation for the same bit pattern. Reading it as a signed byte still requires the signed interpretation rule.