Many programming language provide operators for manipulating the binary (base-2) digits of integers. Here is one way to generalize these operators to other bases:
Let x and y be single-digit numbers in base B. Define the unary operator ~
and binary operators &
, |
, and ^
such that:
- ~x = (B - 1) - x
- x & y = min(x, y)
- x | y = max(x, y)
- x ^ y = (x & ~y) | (y & ~x)
Note that if B=2, we get the familiar bitwise NOT, AND, OR, and XOR operators.
For B=10, we get the “decimal XOR” table:
^ │ 0 1 2 3 4 5 6 7 8 9
──┼────────────────────
0 │ 0 1 2 3 4 5 6 7 8 9
1 │ 1 1 2 3 4 5 6 7 8 8
2 │ 2 2 2 3 4 5 6 7 7 7
3 │ 3 3 3 3 4 5 6 6 6 6
4 │ 4 4 4 4 4 5 5 5 5 5
5 │ 5 5 5 5 5 4 4 4 4 4
6 │ 6 6 6 6 5 4 3 3 3 3
7 │ 7 7 7 6 5 4 3 2 2 2
8 │ 8 8 7 6 5 4 3 2 1 1
9 │ 9 8 7 6 5 4 3 2 1 0
For multi-digit numbers, apply the single-digit operator digit-by-digit. For example, 12345 ^ 24680 = 24655, because:
- 1 ^ 2 = 2
- 2 ^ 4 = 4
- 3 ^ 6 = 6
- 4 ^ 8 = 5
- 5 ^ 0 = 5
If the operands are different lengths, then pad the shorter one with leading zeros.
The challenge
Write, in as few bytes as possible, a program or function that takes as input two integers (which may be assumed to be between 0 and 999 999 999, inclusive) and outputs the “decimal XOR” of the two numbers as defined above.
Test cases
- 12345, 24680 → 24655
- 12345, 6789 → 16654
- 2019, 5779 → 5770
- 0, 999999999 → 999999999
- 0, 0 → 0
09
an acceptable result for an input of90, 99
? \$\endgroup\$A^B^B=A
\$\endgroup\$a^b=b^a
anda^b^b=a
for bases with an odd prime divisor \$\endgroup\$