50102. File Addition

I'm a slow walker, but I never walk backwards.

Write a program to add two big numbers from two binary files.

Task Description

We are given two binary files, and each file contains a big binary number. The task is to add these two numbers together and write the sum to an output binary file. The first byte of a file has the most significant bits of the number it represents.

Let us illustrate the task with two examples. In the first example, we assume that the two binary files 0.bin and 1.bin contain two binary numbers as the following.

0.bin:

00001101 01011110 11110101 01100010 01001110 01101100 00011011 01010101 10010101 11001111

1.bin:

00001100 11100001 10000110 00010011 11001010 11100001 00101010 00101000 10001011 11100010

The two numbers are both 10 bytes. The file position indicator starts from MSB (the most significant bit), so we need to call fseek to reposition the indicator to the last byte first, then we read the two bytes to unsigned character variables and add them. Note that we need to add the numbers from the least significant bits, so we should process the files from the end towards the beginning. There may be a carry after addition, if so, we need to consider it for the next addition. After addition, we also need to call fseek to change the indicator of the output file to the corresponding position and write the sum of the two bytes to the output file. After that, we call fseek to reposition the indicators of input files to the next to last bytes and do addition again. We repeat this process until each byte is calculated.

Note that it is guaranteed that the MSB of the number will not produce a carry when we do addition, so the file length of the output file will be the same as the length of the longer input file.

The sum is as follows:
0.out:

00011010 01000000 01111011 01110110 00011001 01001101 01000101 01111110 00100001 10110001

In the second example, we assume that we are given two binary files 2.bin and 3.bin as the following. There are 9 and 5 bytes in these two files respectively. Note that in this example the lengths of the two files are different.

2.bin:

00001110 00010111 00011001 01001010 01001111 01100010 00110111 10010110 10000100

3.bin:

01111000 10111101 01101111 00001010 00110100

The sum is as follows.
1.out:

00001110 00010111 00011001 01001010 11001000 00011111 10100110 10100000 10111000

Again note that it is guaranteed that the MSB of the number will not produce a carry when we do addition, so the file length of the output file will be the same as the length of the longer input file.

Subtask

  • 20 points: The two binary numbers can be read to memory entirely (Each file is less than 4096 Bytes), and the lengths of the two numbers are the same.
  • 20 points: The two binary numbers can be read to memory entirely (Each file is less than 4096 Bytes), and the lengths of the two numbers may be different.
  • 60 points: The two binary numbers cannot be read to memory entirely, and the lengths of the two numbers may be different.

Input Format

The input contains only one test case. There are three strings. The first and the second strings are the filenames of the input files and the third string is the name of the output file. The filename doesn't contain any whitespaces.
filename length < 32

Output Format

Write the sum to an output binary file.

Sample Input 1 (stdin)

0.bin, 1.bin

0.bin
1.bin
0.out

Sample output 1 (binary file)

0.out

Sample Input 2 (stdin)

2.bin, 3.bin

2.bin
3.bin
1.out

Sample output 2 (binary file)

1.out

Sample Input 3 (stdin)

4.bin, 5.bin

4.bin
5.bin
2.out

Sample output 3 (binary file)

2.out

Discussion