Drill

ProblemsC++ › network

Single-byte checksum of a payload

easynetworkArraysMathC++

Many lightweight protocols use a one-byte checksum: just the sum of all bytes modulo 256.

checksumByte(payload: list<int>) → int

C++ needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

int checksumByte(std::vector<int> payload) {
    
}

Worked examples

CallResult
checksumByte(std::vector<int>{1, 2, 3})6
checksumByte(std::vector<int>{255, 1})0
checksumByte(std::vector<int>{})0
checksumByte(std::vector<int>{128, 128})0

Hint

Add every element, then take the remainder when divided by 256.

Reference solution in C++
int checksumByte(std::vector<int> payload) {
    int sum = 0;
    for (int b : payload) sum += b;
    return sum % 256;
}

The same problem in another language

More network problems in C++