Skip to content
RED SAGSkills

Packet Checksum Byte

A serial protocol (the same one used by Intel HEX files) ends every packet with a one-byte checksum. Add up all data bytes, keep only the lowest 8 bits of the sum, and take the two's complement of that byte: (256 - (sum % 256)) % 256. The receiver adds every byte plus the checksum and expects the low 8 bits to be 0.

Return the checksum as a number from 0 to 255. An empty packet has checksum 0.

Example 1

Input: bytes = [1,2,3]
Output: 250
Explanation: Sum 6, and 256 - 6 = 250.

Example 2

Input: bytes = [3,0,48,0,2,51,122]
Output: 30
Explanation: The record :0300300002337A1E has checksum 0x1E = 30.

Constraints

  • 0 <= bytes.length <= 10^5
  • 0 <= bytes[i] <= 255
Hint 1

Keep the running sum small with & 0xff or % 256 as you go.

Hint 2

The final % 256 turns 256 back into 0.

Good practice for: Embedded Systems Engineer, Blockchain Developer, Backend Developer, Game Developer, Automation Test Engineer

Run checks your code on 3 sample tests. Submit runs all 8 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.