Binary and hexadecimal
Student workbook page 6 · Open lesson worksheet (Word)
One byte has eight positions
Trace the stages from left to right.
- 1128 | 64 | 32 | 16
- 28 | 4 | 2 | 1
- 300000000 → 0
- 411111111 → 255
Discuss before continuing
Predict 255 + 1. Python integers can grow; an unsigned eight-bit field cannot hold 256.
Start · 5 minutes
What does a carry mean in base two? Convert 101 to decimal using place values.
Explain and demonstrate · 10 minutes
Each place is worth twice the place to its right. A byte has eight bits. Unsigned means no negative values: 00000000 is 0 and 11111111 is 255. Hexadecimal groups four binary bits into one digit.
Demonstration
Draw columns 128, 64, 32, 16, 8, 4, 2, 1. Place 00101101 under them and add only the columns containing 1. Split the same byte into 0010 and 1101; use the four-bit values to get 2 and D. In Python, int("00101101", 2) checks decimal, while format(45, "08b") and format(45, "02X") check both representations. Python integers are not limited to one byte; to_bytes adds that constraint.
value = 255
print(value.to_bytes(1, "big")) Class example · 10 minutes
Convert 00101101 to decimal and hexadecimal. Can 256 fit in one unsigned byte?
Reveal example answer
00101101 = 32 + 8 + 4 + 1 = 45. Group 0010 and 1101 to get 2D. One unsigned byte ends at 255, so 256 needs more than eight bits. In Python, (256).to_bytes(1, "big") raises OverflowError.
Your work · 25 minutes
Review place value and conversions. Use the number-systems studio. Find the limits of an unsigned byte. In Python, try converting values just below, at and above each boundary into one unsigned byte. Predict first, run, then explain any error.
Practice steps
Convert 00110110 and 10100101 using a place-value table and grouped nibbles. Predict what (-1), 0, 255 and 256 do with .to_bytes(1, "big"). Run one at a time, record the exact result or error and explain the boundary.
Check and finish · 5 minutes
Explain why the same bit pattern needs an agreed meaning. Save one predicted and actual Python boundary result.
Python practice · two short prompts
Spend about 3 minutes on each during practice time. Predict, run, change one thing, then explain. These prompts do not add homework.
A starter is runnable code to explore or complete. If the playground restores your saved code, the linked starter is selected in Example: use Load example when ready. Download your work before confirming replacement.
Prompt 1
Predict the binary and hexadecimal forms of 45. Change value to 54 and check your place-value work.
No imports needed.
Hint and starter code
bin() and hex() return strings with 0b and 0x prefixes.
# bin() and hex() return strings with 0b and 0x prefixes.
value = 45
print(bin(value))
print(hex(value))
Prompt 2
Complete the condition that tells whether each number fits in one unsigned byte. Test both boundaries.
No imports needed.
Hint and starter code
Use 0 <= value <= 255. A condition produces True or False.
# Use 0 <= value <= 255.
for value in [-1, 0, 255, 256]:
fits = False # Your turn: replace with a condition
print(value, fits)