Padding binary with leading zeros

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Padding binary with leading zeros

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which is the 8-bit way to write the number 5 in binary?
A 00000101
B 10100000
2. Adding leading zeros to a binary number changes its value.
A True
B False
3. Do 101 and 00000101 mean the same number?
A No, 00000101 is bigger
B Yes, both are 5
4. Why do computers pad binary codes to a fixed length like 8 bits?
A So every code is the same size and is easy to separate and read
B So the number becomes bigger
5. Where do you add the extra 0s to pad 11 to 8 bits?
A On the left: 00000011
B On the right: 11000000

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
100000101 A01000001
20011 BThe number 5 written as an 8-bit binary number, with five leading zeros added to fill all 8 bits
3Leading zeros do not change the value of a binary number, so these two numbers are both 5 CThe zeros were added on the right instead of the left. That changes the value to 160. Padding must go on the left: 00000101.
4Fixed-length codes make every character the same size, so 'A' (65) is stored in 8 bits with a leading zero D101 = 00000101
55 in 8 bits = 10100000 EThis has 6 bits, not 4, and the zeros are on the right. Padding 110 to 4 bits should give 0110.
66 in 4 bits = 110000 FThe number 3 written in 4 bits, with two leading zeros padding the binary number 11

Definitions

leading zeros

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
101 padded to 8 bits = _____101
2.
00000101 and 101 both have the value _
3.
Pad 11 to 4 bits: __11
4.
Leading zeros do not change the _____
5.
Each character is padded to 8 bits. 3 characters = 3 x 8 = __ bits

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A n = 5
B bits = format(n, "8b")
C print(bits)
Question 2
A n = 5
B bits = bin(n)[2:].zfill(3)
C print(bits)
Question 3
A width = 3
B code = "101"
C padded = code.zfill(width)
D print(padded)
Question 4
A code = 101
B zeros = "0" * (8 - len(code))
C print(zeros + code)
Question 5
A bits = "101"
B size = 8
C zeros = size - len(bits)
D padded = bits + "0" * zeros
E print(padded)

Answer Key

Padding binary with leading zeros
Section A: Quick Questions
Q1
A — 00000101
Q2
B — False
Q3
B — Yes, both are 5
Q4
A — So every code is the same size and is easy to separate and read
Q5
A — On the left: 00000011
Section B: Match the Pairs
Q1
00000101 → The number 5 written as an 8-bit binary number, with five leading zeros added to fill all 8 bits
Q2
0011 → The number 3 written in 4 bits, with two leading zeros padding the binary number 11
Q3
Leading zeros do not change the value of a binary number, so these two numbers are both 5 → 101 = 00000101
Q4
Fixed-length codes make every character the same size, so 'A' (65) is stored in 8 bits with a leading zero → 01000001
Q5
5 in 8 bits = 10100000 → The zeros were added on the right instead of the left. That changes the value to 160. Padding must go on the left: 00000101.
Q6
6 in 4 bits = 110000 → This has 6 bits, not 4, and the zeros are on the right. Padding 110 to 4 bits should give 0110.
Section C: Definitions
Q1
leading zeros — Extra 0s added at the left of a binary number to fill a fixed number of bits. They do not change its value: 101 and 00000101 both mean 5.
Section D: Fill in the Blanks
Q1
101 padded to 8 bits = 00000101
Q2
00000101 and 101 both have the value 5
Q3
Pad 11 to 4 bits: 0011
Q4
Leading zeros do not change the value
Q5
Each character is padded to 8 bits. 3 characters = 3 x 8 = 24 bits
Section E: Spot the Error
Q1
B — Without the 0 in the format code, the number is padded with spaces instead of zeros. It should be "08b" so 5 becomes 00000101.
Q2
B — Padding to a width of 3 leaves 101 unchanged. An 8-bit code needs zfill(8) to give 00000101.
Q3
A — The fixed length is 8 bits, so width must be 8. A width of 3 adds no leading zeros because 101 is already 3 bits long.
Q4
A — len() only works on a string, and 101 without quotes is a number. It should be code = "101" so the bits can be counted and padded.
Q5
D — The zeros must go on the left of the number as leading zeros. Putting them on the right gives 10100000, which is 160, not 5.