Leading zeros and fixed-width binary

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

Leading zeros and fixed-width binary

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which shows 101 padded to 8 bits?
A 10100000
B 00000101
2. Does adding leading zeros change the value of a binary number?
A No, 0101 and 101 are both 5
B Yes, 0101 is bigger than 101
3. What is 11 written as a 4-bit binary number?
A 0011
B 1100
4. How many bits are in 00001111?
A 4 bits
B 8 bits
5. What is the value of 00000101?
A 5
B 101

Match the Pairs

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

Left columnRight column
100000101 AThe binary number 101 padded with leading zeros to fill 8 bits. Its value is still 5.
20110 BThe zeros were added on the right, which changes the value to 160. Padding zeros must go on the left: 00000101.
3Writing 11 as a 4-bit binary group by adding leading zeros C0011
4Writing 1001 as an 8-bit binary number by padding with zeros on the left DA 4-bit group holding the binary number 110, which is 6 in denary.
5101 padded to 8 bits: 10100000 E00001001
60011 is bigger than 11 because it has more digits FLeading zeros add nothing. 0011 and 11 are both 3, so the value is the same.

Definitions

00000101

Fill in the Blanks

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

1.
101 as 8 bits: _____101
2.
=DEC2BIN(5,_)
3.
3 in 4 bits is __11
4.
00000101 has the same value as ___
5.
=_______(5,8)

Spot the Error

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

Question 1
A n = 5
B bits = bin(n)[2:]
C padded = bits.zfill(8)
D print(len(padded) == 4)
Question 2
A n = 5
B bits = format(n, '08')
C print(bits)
D print(int(bits, 2))
Question 3
A n = 6
B nibble = format(n, '04b')
C print(int(nibble, 10))
Question 4
A a = int('101', 2)
B b = int('00010101', 2)
C print(a == b)
Question 5
A bits = 101
B padded = bits.zfill(8)
C print(padded)

Answer Key

Leading zeros and fixed-width binary
Section A: Quick Questions
Q1
B — 00000101
Q2
A — No, 0101 and 101 are both 5
Q3
A — 0011
Q4
B — 8 bits
Q5
A — 5
Section B: Match the Pairs
Q1
00000101 → The binary number 101 padded with leading zeros to fill 8 bits. Its value is still 5.
Q2
0110 → A 4-bit group holding the binary number 110, which is 6 in denary.
Q3
Writing 11 as a 4-bit binary group by adding leading zeros → 0011
Q4
Writing 1001 as an 8-bit binary number by padding with zeros on the left → 00001001
Q5
101 padded to 8 bits: 10100000 → The zeros were added on the right, which changes the value to 160. Padding zeros must go on the left: 00000101.
Q6
0011 is bigger than 11 because it has more digits → Leading zeros add nothing. 0011 and 11 are both 3, so the value is the same.
Section C: Definitions
Q1
00000101 — 00000101 is the binary number 101 padded with leading zeros to fill 8 bits. Zeros added on the left do not change the value, so it is still 5.
Section D: Fill in the Blanks
Q1
101 as 8 bits: 00000101
Q2
=DEC2BIN(5,8)
Q3
3 in 4 bits is 0011
Q4
00000101 has the same value as 101
Q5
=DEC2BIN(5,8)
Section E: Spot the Error
Q1
D — zfill(8) pads the binary number to 8 bits, so the length check should compare with 8, not 4.
Q2
B — The format code needs the letter b to give binary. '08b' gives 00000101 (8 bits, padded with zeros). '08' would just give a decimal number.
Q3
C — The nibble 0110 is binary, so int() must use base 2. Base 10 reads it as the decimal number 110 instead of 6.
Q4
B — The 8-bit version of 101 is 00000101. Writing 00010101 changes a bit to a 1 (it is 10101, which is 21), so it is no longer equal to 5.
Q5
A — The binary number must be a string such as '101' so zfill can add zeros on the left. 101 without quotes is a plain integer and has no zfill.