This question already has an answer here:
- Add two numbers 188 answers
The C preprocessor is not turing-complete, but you can do some pretty cool stuff with it.
The challenge is to make an integer adder with it. You can use any representation you like: base 2, base 10, unary, separate each digit with a comma, etc. Whatever you do, you may only use the C preprocessor.
The rules are simple:
- You can only use the C preprocessor.
- You must support both positive and negative integers.
- You can use any integer representation you like, both for input and output. You must clearly state what representation(s) you're using. The input representation doesn't have to match the output representation.
- CRLF counts as 2 bytes, LF counts as 1. Convert your files to LF line endings.
- You must actually calculate the values, that is, they should neither be hardcoded nor output as
a+b. This is a C preprocessor challenge, not a C challenge.
- You must have an
ADDmacro that takes two numbers and outputs one. The numbers must be comma-separated. (for example
ADD(a1,a2,a3,...,a32,b1,b2,b3,...,b32)for 32-bit fixed-width binary input.)
Possible inputs and their outputs, in decimal:
- 133, 57 (= 190)
- 123, 321 (= 444)
- 111, 289 (= 400)
- 9, 9 (= 18)
- 18, 18 (= 36)
- 9, -9 (= 0)
- -9, -9 (= -18)
- 0, 9 (= 9)
- -9, 0 (= -9)
- 399, -400 (= -1)