Calculating a checksum

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

Calculating a checksum

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. How is a simple checksum calculated for a block of data?
A Add up all the byte values and keep the result within one byte
B Multiply all the byte values together and keep the first digit
2. Which calculation keeps a total inside one byte?
A total % 256
B total % 100
3. A block contains the bytes 200, 100 and 50. What is the checksum?
A 350
B 94
4. A block contains the bytes 10, 20, 30 and 40. What is the checksum?
A 100
B 244
5. The bytes 255 and 1 are added. What checksum do we get?
A 256
B 0

Match the Pairs

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

Left columnRight column
1checksum = sum(bytes) % 256 Achecksum = total MOD 256
2350 % 256 BThe bytes are added but the total is never reduced. 350 is greater than 255, so it does not fit in one byte; it needs % 256 to give 94.
3The first step of a checksum is to add the values of all the bytes in the block together. CThe total is divided by 255 instead of 256. One byte holds 256 different values, so the modulus must be 256 (the correct answer is 94).
4If the total is more than 255, only the low byte is kept by taking the remainder after dividing by 256. Dtotal = 12 + 30 + 8 + 50
5checksum = (200 + 100 + 50) % 255 EAdd up every byte in the block, then keep the remainder after dividing by 256 so the checksum fits in one byte.
6checksum = 200 + 100 + 50 FGives 94, because 350 is too big for one byte and 350 - 256 leaves 94.

Definitions

checksum = sum(bytes) % 256

Fill in the Blanks

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

1.
checksum = ___(bytes) % 256
2.
checksum = sum(bytes) _ 256
3.
checksum = sum(bytes) % ___
4.
bytes = [200, 100, 50] total = sum(bytes) checksum = _____ % 256
5.
# 200 + 100 + 50 = 350 checksum = 350 % 256 print(checksum) # __

Spot the Error

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

Question 1
A data = [200, 100, 50]
B total = len(data)
C checksum = total % 256
D print(checksum)
Question 2
A a = 200
B b = 100
C total = a * b
D checksum = total % 256
Question 3
A total = 0
B for b in [200, 100, 50]
C total += b
D print(total % 256)
Question 4
A data = [120, 80, 100
B total = sum(data)
C print(total % 256)
Question 5
A data = [255, 1, 2]
B total = sum(data)
C checksum = total // 256
D print(checksum)

Answer Key

Calculating a checksum
Section A: Quick Questions
Q1
A — Add up all the byte values and keep the result within one byte
Q2
A — total % 256
Q3
B — 94
Q4
A — 100
Q5
B — 0
Section B: Match the Pairs
Q1
checksum = sum(bytes) % 256 → Add up every byte in the block, then keep the remainder after dividing by 256 so the checksum fits in one byte.
Q2
350 % 256 → Gives 94, because 350 is too big for one byte and 350 - 256 leaves 94.
Q3
The first step of a checksum is to add the values of all the bytes in the block together. → total = 12 + 30 + 8 + 50
Q4
If the total is more than 255, only the low byte is kept by taking the remainder after dividing by 256. → checksum = total MOD 256
Q5
checksum = (200 + 100 + 50) % 255 → The total is divided by 255 instead of 256. One byte holds 256 different values, so the modulus must be 256 (the correct answer is 94).
Q6
checksum = 200 + 100 + 50 → The bytes are added but the total is never reduced. 350 is greater than 255, so it does not fit in one byte; it needs % 256 to give 94.
Section C: Definitions
Q1
checksum = sum(bytes) % 256 — Add all the byte values in the block, then keep the remainder after dividing by 256 so the result fits in one byte. Example: 200+100+50=350, 350 mod 256 = 94.
Section D: Fill in the Blanks
Q1
checksum = sum(bytes) % 256
Q2
checksum = sum(bytes) % 256
Q3
checksum = sum(bytes) % 256
Q4
bytes = [200, 100, 50] total = sum(bytes) checksum = total % 256
Q5
# 200 + 100 + 50 = 350 checksum = 350 % 256 print(checksum) # 94
Section E: Spot the Error
Q1
B — len(data) only counts how many bytes there are (3). A checksum needs the sum of the byte values, so it should be total = sum(data).
Q2
C — A checksum is found by adding the bytes, not multiplying them. It should be total = a + b.
Q3
B — A for loop header must end with a colon. It should be for b in [200, 100, 50]:
Q4
A — The list is missing its closing square bracket. It should be data = [120, 80, 100].
Q5
C — // gives the whole-number quotient, not the remainder. The checksum is the remainder after dividing by 256, so use %.