The Internet checksum

Compute the 16-bit Internet checksum used by IP, UDP and TCP. Add the words with one's complement addition: whenever the sum goes past 16 bits, add the carry back in at the bottom. The checksum is that sum with every bit flipped.

Input

A line with n (1 ≤ n ≤ 100), then a line with n words, each 4 hex digits.

Output

The checksum as 4 lowercase hex digits.

Examples

Example 1 · Input
9
4500 0073 0000 4000 4011 c0a8 0001 c0a8 00c7
Expected output
b861
Example 2 · Input
2
ffff ffff
Expected output
0000

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Read the words with int(word, 16).

Hint 2

After each addition, wrap any carry around: total = (total & 0xFFFF) + (total >> 16).

Hint 3

The checksum is the sum with every bit flipped: total ^ 0xFFFF. Print it with f"{value:04x}".

Solution

Show one correct solution (try it yourself first)
n = int(input())
words = [int(w, 16) for w in input().split()]
total = 0
for w in words:
    total += w
    total = (total & 0xFFFF) + (total >> 16)
print(f"{total ^ 0xFFFF:04x}")

Lesson: Detecting errors: parity, checksums and CRC →