Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Decimal "XOR" operator

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

Answer*

Draft saved
Draft discarded
Cancel
2
  • \$\begingroup\$ 76 bytes. I think this question might be a unique opportunity to use Zip. \$\endgroup\$ Commented Sep 17, 2019 at 4:02
  • 1
    \$\begingroup\$ @someone Thanks! Regarding Zip, you can't use it as it automatically truncates the longer collection to the length of the shorter one \$\endgroup\$ Commented Sep 17, 2019 at 4:03

AltStyle によって変換されたページ (->オリジナル) /