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*

XOR two strings

Given two strings as input, return the result of XORing the code-points of one string against the code points of the other.

For each character in the first input string, take the code-point (e.g. for A, this is 65) and XOR the value against the corresponding index in the second string and output the character at the code-point of the result. If one string is longer than the other, you must return the portion of the string beyond the length of the shorter, as-is. (Alternatively, you may pad the shorter string with NUL bytes, which is equivalent.)

See the following JavaScript code for an example:

const xorStrings = (a, b) => {
 let s = '';
 // use the longer of the two words to calculate the length of the result
 for (let i = 0; i < Math.max(a.length, b.length); i++) {
 // append the result of the char from the code-point that results from
 // XORing the char codes (or 0 if one string is too short)
 s += String.fromCharCode(
 (a.charCodeAt(i) || 0) ^ (b.charCodeAt(i) || 0)
 );
 }
 return s;
};

Try it online!

Test cases

Input Output
['Hello,', 'World!'] '\x1f\x0a\x1e\x00\x0b\x0d'
['Hello', 'wORLD'] '?*> +'
['abcde', '01234'] 'QSQWQ'
['lowercase', "9?' "] 'UPPERCASE'
['test', ''] 'test'
['12345', '98765'] '\x08\x0a\x04\x02\x00' _not_ 111092
['test', 'test'] '\x00\x00\x00\x00'
['123', 'ABCDE'] 'pppDE'
['01', 'qsCDE'] 'ABCDE'
['`c345', 'QQ'] '12345'

Rules

  • The two input strings will only ever be code-points 0-255.
  • This is so the shortest solution, in each language, wins.

Answer*

Draft saved
Draft discarded
Cancel
10
  • \$\begingroup\$ I didn't even know about -Mbitwise, but I think you can drop it (and the .). For single inputs (which is fine IMO), your intended idea should work too: Try it online! \$\endgroup\$ Commented Jul 17, 2020 at 11:10
  • 2
    \$\begingroup\$ @DomHastings Yeah, but that relies on having the last line of STDIN to not be terminated with a newline, which is kind of icky (and hard to spot when looking at test input). BTW, it's not -Mbitwise, but -Mfeature=bitwise. \$\endgroup\$ Commented Jul 17, 2020 at 11:30
  • \$\begingroup\$ That's very true, it does! Without the -Mfeature=bitwise though, you can avoid the . in ^. for -1! \$\endgroup\$ Commented Jul 17, 2020 at 12:39
  • 2
    \$\begingroup\$ When testing, I was running the program as perl -M5.032 -nl program.pl < input. This turns on strict, which prohibits an undeclared $x (or any other single letter variable). $; however is always a package variable. \$\endgroup\$ Commented Jul 17, 2020 at 18:20
  • 1
    \$\begingroup\$ @Abigail The challenge specifies input is in the range 0-255. So I think you have to account for newlines in the strings. Maybe do it as a subroutine instead of a standalone script (is that still legal here?), something like pop^.pop? \$\endgroup\$ Commented Jul 19, 2020 at 7:43

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