CRC remainder

Compute a CRC. Append r zeros to the data, where r is one less than the generator's length, and divide by the generator in arithmetic mod 2 (XOR instead of subtraction). The remainder is the CRC; the sender transmits the data followed by it.

Input

Two lines: the data (1 to 64 bits) and the generator (2 to 17 bits, starting with 1), written with 0 and 1.

Output

Two lines: the remainder (r bits), and the bits sent (the data followed by the remainder).

Examples

Example 1 · Input
1101011011
10011
Expected output
1110
11010110111110
Example 2 · Input
1
11
Expected output
1
11

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

Hints

Hint 1

Append len(generator) - 1 zeros to the data.

Hint 2

Walk left to right: wherever the current bit is 1, XOR the generator into the bits starting there.

Hint 3

The remainder is the last len(generator) - 1 bits.

Solution

Show one correct solution (try it yourself first)
data = input().strip()
gen = input().strip()
r = len(gen) - 1
bits = list(data + "0" * r)
for i in range(len(data)):
    if bits[i] == "1":
        for j in range(len(gen)):
            bits[i + j] = "0" if bits[i + j] == gen[j] else "1"
remainder = "".join(bits[len(data):])
print(remainder)
print(data + remainder)

Lesson: Detecting errors: parity, checksums and CRC →