Serial cables pick up noise, flash storage flips bits, and buggy firmware writes a byte in the wrong place. Without a check, the decoder would turn a damaged frame into a plausible but wrong reading, and nothing downstream could tell. The frame's last octet is a checksum: the sum of the nine preceding octets, modulo 256. The decoder recomputes it and rejects the frame on a mismatch.
It helps to be precise about what that buys. A checksum is evidence that the bytes probably were not damaged by accident. It is not proof that they are correct, and it says nothing about who sent them. This lesson shows both the arithmetic and its limits, so you know which guarantees the decoder can and cannot claim.
Predict and run
Read the program before pressing Run. Predict the printed number and explain the wraparound.
#include <stdio.h>
unsigned char sum8(const unsigned char *bytes, size_t size) {
unsigned char sum = 0;
for (size_t i = 0; i < size; ++i) { sum = (unsigned char)(sum + bytes[i]); }
return sum;
}
int main(void) {
unsigned char bytes[3] = {0xff,1,2};
printf("%u\n", (unsigned)sum8(bytes,3));
return 0;
}Check your prediction
2255 + 1 is 256, which reduces to 0 modulo 256, and adding 2 gives 2. Look closely at sum + bytes[i]. Both operands are unsigned char, but C promotes them to int before adding, so the addition itself is done in int and can reach at most 255 + 255 = 510. That always fits, since int holds at least 32767. The cast back to unsigned char is where the modulo happens: converting to an unsigned type is defined to wrap. The explicit cast also tells readers, and -Wconversion, that the narrowing is intended.
What the checksum misses
A sum ignores order, and changes that cancel each other leave it unchanged. Both are easy to show with the known fixture for sensor 1, value 25:
#include <stdio.h>
static unsigned sum9(const unsigned char *f) {
unsigned char sum = 0;
for (size_t i = 0; i < 9; ++i) { sum = (unsigned char)(sum + f[i]); }
return sum;
}
int main(void) {
const unsigned char fixture[10] = {0x54, 1, 1, 0, 4, 0, 1, 0, 25, 0x74};
const unsigned char swapped[10] = {0x54, 1, 1, 0, 4, 0, 25, 0, 1, 0x74};
const unsigned char cancel[10] = {0x54, 1, 1, 0, 4, 0, 2, 0, 24, 0x74};
printf("fixture: %02X\n", sum9(fixture));
printf("swapped: %02X\n", sum9(swapped));
printf("cancel: %02X\n", sum9(cancel));
return 0;
}fixture: 74
swapped: 74
cancel: 74The swapped frame says sensor 25, value 1. The cancel frame says sensor 2, value 24. Both pass the checksum and both are inside the valid ranges, so the decoder would accept either one as a genuine reading. With only 256 possible checksum values, roughly one random corruption in 256 slips through too. Stronger codes such as CRC-16 or CRC-32 catch reordering and burst errors far more reliably, which is why real link protocols use them. This course keeps the simple sum so you can verify it by hand.
Not a security boundary
Anyone who can change the bytes can also recompute the checksum. It is not encryption, so it hides nothing, and it is not a signature, so it proves nothing about the sender. If a real system needed to trust telemetry from an untrusted network, it would need a message authentication code with a secret key. Writing "checksum verified" in a log is fine; writing "frame authenticated" would be a false claim.
Validate everything, then publish once
A frame that passes the checksum still has to pass the domain rules: sensor 1 to 4095 and value 0 to 10000. The decoder decodes into a local candidate, checks it, and writes the caller's output in a single assignment only when every check has passed. If it wrote out->sensor before checking the value, a rejected frame would leave the caller holding half of a new reading and half of an old one, a combination that never existed on the wire.
Practice
Your turn
Write int check_frame(const unsigned char f[10], Reading *out) that returns 0 for ok, 1 for a checksum mismatch and 2 for an out-of-range field, and assigns *out only on ok. Run it on the four frames in the starter, resetting out to {77, 88} before each, and print:
fixture: ok, out 1 25
bit flip: checksum mismatch, out 77 88
swapped: ok, out 25 1
sensor zero: out of range, out 77 88#include <stdio.h>
typedef struct { unsigned sensor; unsigned value; } Reading;
int main(void) {
const unsigned char frames[4][10] = {
{0x54, 1, 1, 0, 4, 0, 1, 0, 25, 0x74},
{0x54, 1, 1, 0, 4, 0, 1, 0, 24, 0x74},
{0x54, 1, 1, 0, 4, 0, 25, 0, 1, 0x74},
{0x54, 1, 1, 0, 4, 0, 0, 0, 25, 0x73},
};
const char *labels[4] = {"fixture", "bit flip", "swapped", "sensor zero"};
/* Write check_frame above and print one line per frame. */
(void)frames; (void)labels;
return 0;
}Hint 1: the idea
Run the checks in order: checksum over octets 0 to 8 compared with octet 9, then decode the candidate, then the ranges. Only the last line of the success path touches *out.
Hint 2: a first step
Start with the checksum test alone and run the starter's frames: the bad-checksum frame should be the only one rejected. Then build a local candidate from octets 5 to 8, two octets per field with the high byte first, and range-check the candidate before anything touches *out.
Show one solution
#include <stdio.h>
typedef struct { unsigned sensor; unsigned value; } Reading;
int check_frame(const unsigned char f[10], Reading *out) {
unsigned char sum = 0;
for (size_t i = 0; i < 9; ++i) { sum = (unsigned char)(sum + f[i]); }
if (sum != f[9]) { return 1; }
Reading candidate = {((unsigned)f[5] << 8) | f[6], ((unsigned)f[7] << 8) | f[8]};
if (candidate.sensor < 1 || candidate.sensor > 4095 || candidate.value > 10000) { return 2; }
*out = candidate;
return 0;
}
int main(void) {
const unsigned char frames[4][10] = {
{0x54, 1, 1, 0, 4, 0, 1, 0, 25, 0x74},
{0x54, 1, 1, 0, 4, 0, 1, 0, 24, 0x74},
{0x54, 1, 1, 0, 4, 0, 25, 0, 1, 0x74},
{0x54, 1, 1, 0, 4, 0, 0, 0, 25, 0x73},
};
const char *labels[4] = {"fixture", "bit flip", "swapped", "sensor zero"};
const char *results[3] = {"ok", "checksum mismatch", "out of range"};
for (size_t i = 0; i < 4; ++i) {
Reading out = {77, 88};
int result = check_frame(frames[i], &out);
printf("%s: %s, out %u %u\n", labels[i], results[result], out.sensor, out.value);
}
return 0;
}fixture: ok, out 1 25
bit flip: checksum mismatch, out 77 88
swapped: ok, out 25 1
sensor zero: out of range, out 77 88The sensor-zero frame has a repaired checksum, 0x73, so its rejection comes from the domain check alone. That is how you prove the two checks are independent: a test where one passes and the other fails. The swapped line is the honest limit of the design, a corrupted frame accepted as valid. The checkpoint next assembles these checks into a full encoder and decoder for one frame.
Completion is saved in this browser only. It does not measure mastery.