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'


  • The two input strings will only ever be code-points 0-255.
  • This is so the shortest solution, in each language, wins.
  • 2
    \$\begingroup\$ You might want to add a few other test cases of varying length besides just ['test', '']. I see many answers being posted which fail for this test case due to the length difference (or because one of the two is empty maybe). \$\endgroup\$ Jul 17, 2020 at 9:13
  • 1
    \$\begingroup\$ @KevinCruijssen Done and emboldened (and hopefully added a viable alternative) the detail. \$\endgroup\$ Jul 17, 2020 at 9:25
  • 1
    \$\begingroup\$ Alternatively, padding the shorter string with NUL bytes is equivalent. Can you confirm that you mean we can, if we like, assume that the two strings will be of the same length with the originally shorter one padded with NUL characters? Or does this mean that the implementation should do the padding, and that this is equivalent to the requirement to "return the portion of the string beyond the length of the shorter, as-is"? \$\endgroup\$ Jul 17, 2020 at 17:54
  • 3
    \$\begingroup\$ @user7761803 I've edited to clarify that I meant it should be padded as part of your answer. Hopefully it's clear now! \$\endgroup\$ Jul 17, 2020 at 19:40
  • 1
    \$\begingroup\$ That JavaScript implementation doesn't seem correct, since it won't work with code points that encode to surrogate pairs in UTF-16. \$\endgroup\$
    – D. Pardal
    Jul 19, 2020 at 9:00

26 Answers 26


Jelly, 4 bytes


Try it online!

Takes input as a list of the two strings, e.g. ['abcde', '01234'].


O    # ord: cast to number (automatically vectorizes)
 ^/  # Reduce by XOR. XOR automatically applies to corresponding elements
         and pads as desired to work if the two strings are different lengths
   Ọ # chr: cast to character (vectorizes once again)

Raku, 4 bytes


Try it online!

Raku has a built-in operator for XORing strings, along with string AND, OR and bitshift. This is a Whatever lambda that takes two parameters.


perl -Mfeature=say,bitwise -nl, 22 bytes


Try it online!

This is way more characters than I first hoped for. If it weren't for those pesky newlines, the 9 character say<>^.<> would do.

How does it work?

For odd input lines, it saves the current line of input (without the trailing newline due to the -n and -l switches) into $;. For even lines, it xors the previous line ($;) and the current line ($_), and prints it. The ^. operator does required bitwise string operation.

  • \$\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\$ 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\$
    – Abigail
    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\$ 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\$
    – Abigail
    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\$
    – msh210
    Jul 19, 2020 at 7:43

APL (Dyalog Unicode), 15 bytes


Try it online!

As the OP clarified that the input codepoints will be in the range of 0-255, it is possible to manipulate the underlying data bits directly. Such a string is guaranteed to have data type 80 (8-bit char array), so we convert it to data type 11 (1-bit boolean array) to access the bits, XOR them, and convert back to data type 80.

80⎕DR≠⌿↑11⎕DR¨⎕  ⍝ Full program, input: two string literals on a line
        11⎕DR¨⎕  ⍝ Convert each string literal to bit array
       ↑         ⍝ Promote to matrix, padding with 0 as needed
     ≠⌿          ⍝ Bitwise XOR
80⎕DR            ⍝ Convert back to 8-bit char array

APL (Dyalog Extended), 17 bytes


Try it online!

Well, the task involves converting char to charcode and back AND converting from/to binary, but all current implementations having have some quirks so it can't be used here. So here is the very literal implementation of the task.

⎕UCS⊥≠⌿⍤2⊤↑⎕UCS¨⎕  ⍝ Full program, input: two string literals on one line
           ⎕UCS¨⎕  ⍝ Convert to codepoints
          ↑        ⍝ Promote into a 2-row matrix, padding zeros as necessary
                   ⍝ (doing on characters give spaces which is 0x20, not 0)
         ⊤  ⍝ Convert each number to binary
     ≠⌿⍤2   ⍝ Bitwise XOR
    ⊥       ⍝ Convert the binary back to integers
⎕UCS        ⍝ Convert the integers back to chars

J, 14 bytes


Try it online!

How it works

        (3&u:) strings -> code points
      &.       do right part, then left part, then the inverse of the right part
    ,:         pad shorter one with zeros by making a table
XOR@           XOR the code points
        (3&u:) revert back code points -> string

C (gcc), 60\$\cdots\$ 55 54 bytes

Saved 2 4 bytes thanks to AZTECCO!!!

Saved a 2 bytes thanks to ceilingcat!!!

#define f(a,b)for(;*a+*b;b+=!!*b)a+=putchar(*a^*b)!=*b

Try it online!

  • \$\begingroup\$ @AZTECCO Unfortunately, that doesn't print the end of the longer string. But found a way that does - thanks! :-) \$\endgroup\$
    – Noodle9
    Jul 18, 2020 at 22:00
  • \$\begingroup\$ Oh my bad, you can save 1 more btw Try it online! \$\endgroup\$
    Jul 19, 2020 at 1:51
  • \$\begingroup\$ And one more using macros Try it o \$\endgroup\$
    Jul 19, 2020 at 2:12
  • \$\begingroup\$ @AZTECCO Nice one - thanks! :-) \$\endgroup\$
    – Noodle9
    Jul 19, 2020 at 9:21
  • \$\begingroup\$ @ceilingcat Of course, nice one - thanks! :D \$\endgroup\$
    – Noodle9
    Jul 20, 2020 at 18:06

Python 3.8, 71 bytes

f=lambda a,b:chr(ord(a[0])^ord(b[0]))+f(a[1:],b[1:])if a and b else a+b

Try it online!

  • 2
    \$\begingroup\$ Nice solution! You can save 2 bytes by shortening the base case as f=lambda a,b:a>''<b and chr(ord(a[0])^ord(b[0]))+f(a[1:],b[1:])or a+b \$\endgroup\$
    – xnor
    Jul 20, 2020 at 4:13
  • \$\begingroup\$ print(''.join(chr(ord(x)^ord(y))for x,y in zip(*input().split()))) would be only 66 bytes but unfortunately, making it work on strings of different lengths would require many more bytes; and it doesn't handle strings that contain whitespace :-( \$\endgroup\$
    – Stef
    Dec 19, 2023 at 16:22

05AB1E, 8 7 bytes

thanks to Kevin Cruijssen for a byte!


Try it online!


	 implicit input          ["QQ", "`c345"]

Ç        convert to charcodes    [[96, 99, 51, 52, 53], [81, 81]]
  ζ      Zip with filler ...     [[96, 81], [99, 81], [51, "0"], [52, "0"], [53, "0"]]
 0       ... zero
   ε     Map ...                   [96, 81]
    `      Dump on stack           96, 81
     ^     XOR                     49
      ç    Convert to character    "1"

         implicit output         ["1", "2", "3", "4", "5"]
  • \$\begingroup\$ 7 bytes by taking the input as a pair of strings. \$\endgroup\$ Jul 17, 2020 at 11:43

Excel, 158 153 108 bytes

(Not counting closing parens)

Compatibility Notes:

  • Minimum version: CONCAT() came to be in later versions of Excel 2016 (from CONCATENATE()).

The Formulae

  • Inputs: A1, B1
  • A2: =MIN(LEN(A1:B1)), 14
  • B2: =LEN(A1)-LEN(B1), 15

Code (124):


One unfortunate caveat is that Excel ignores non-printable characters in cells. Alternatively, if you'd rather I use "\xXX" characters, I have this:


at 118 bytes. This just prints all XOR'ed characters as "\xXX" characters and leaves the trailing characters alone. Eg: Hello! and World!! produce \x3F\x2A\x3E\x20\x2B\x00!

How it Works:

  1. The SEQUENCE(A2) effectively creates a range of (1..A2). As far as I can tell, I cannot re-use this by caching it in a cell, which is why I had to use it twice.
  2. Each item is then converted to numbers with CODE() and BITXOR()ed against each other.
  3. The CHAR() converts this to a character, while DEC2HEX(...,2) converts it to a 2 width 0-padded hex number.
  4. CONCAT() puts the array together
  5. RIGHT(...) tacks on the trailing characters of the longer string.
  • 1
    \$\begingroup\$ May want to throw in that as this uses the BitXOr function, this is limited to Excel 2013 and later \$\endgroup\$ Jul 19, 2020 at 1:49
  • 1
    \$\begingroup\$ @TaylorScott True, but I think the minimum compatibility is actually 2019, because CONCATENATE() was changed to CONCAT() \$\endgroup\$ Jul 19, 2020 at 2:10
  • \$\begingroup\$ You're right - Concat() is the limiting function here, but I think it limits to Office v 16.0.xx which would include Office 2016, 2019 and 365. At least I can confirm that it is functional in up-to date versions of Excel 2016 on Win 10 \$\endgroup\$ Jul 19, 2020 at 3:11
  • 1
    \$\begingroup\$ Yup, you're right. support.microsoft.com/en-us/office/… \$\endgroup\$ Jul 19, 2020 at 3:28

Java 10, 109 bytes

(a,b)->{int A=a.length,B=b.length;if(A<B){var t=a;a=b;b=t;A^=B^(B=A);}for(;A-->0;)a[A]^=A<B?b[A]:0;return a;}

I/O as arrays of characters.

Try it online.


(a,b)->{             // Input as 2 character arrays as parameters as well as return-type
  int A=a.length,    //  `A`: the length of the first array `a`
      B=b.length;    //  `B`: the length of the second array `b`
  if(A<B){           //  If the length of `a` is smaller than `b`:
    var t=a;a=b;b=t; //   Swap the arrays `a` and `b`
    A^=B^(B=A);}     //   And also swap the lengths `A` and `B`
                     //  (`a`/`A` is now the largest array, and `b`/`B` the smallest)
  for(;A-->0;)       //  Loop index `A` in the range [`A`, 0):
    a[A]^=           //   Bitwise-XOR the `A`'th value in `a` with, and implicitly cast
                     //   from an integer codepoint to a character afterwards:
      A<B?           //    If index `A` is still within bounds for `b`:
       b[A]          //     XOR it with the `A`'th codepoint of `b`
      :              //    Else:
       0;            //     XOR it with 0 instead
  return a;}         //  Return the modified `a` as result

Note that we cannot use a currying lambda a->b-> here, because we modify the inputs when swapping and they should be (effectively) final for lambdas.


Factor, 48 bytes

: f ( s s -- s ) 0 pad-longest [ bitxor ] 2map ;

Try it online!


JavaScript (Node.js), 66 bytes


Try it online!


Scala, 64 bytes


Try it online!

Run with

val f: ((String,String)=>String) = ...

Uses zipAll to zip the input strings with null bytes as padding, then XORs, finally using foldLeft shorthand /: to turn the whole thing back into a string.

  • \$\begingroup\$ 60 bytes? \$\endgroup\$
    – user
    Sep 7, 2020 at 13:07

Japt, 6 bytes


Try it

cÈ^VcY     :Implicit input of strings U & V
c          :Map the charcodes in U
 È         :by passing each one at index Y through the following function
  ^        :  Bitwise XOR with
   VcY     :  Charcode at index Y in V

Lua, 105 bytes

i,a,b=0,...print(a:gsub('.',load'i=i+1return a.char((...):byte()~(b:sub(i,i):byte()or 0))')..b:sub(#a+1))

Try it online!

Taking two strings as arguments, this program calls per-character replace on one of them with essentially XOR function, then appends potentially missing fragment from second string (occurs if it is longer) and prints the result. TIO includes test suite.


PHP, 36 bytes



$ php -r '[,$a,$b]=$argv;echo"$a\r$b\r",$a^$b;' -- 'ABCDE' '123';echo
> pppDE

Explanation: first output string A, then carriage return \r, output string B, then another carriage return, then output the XOR (which truncates to the shorter of the 2 strings). Any characters of the longer string will have already been printed.

PHP 7.4, 32 bytes

Using new arrow function syntax.


Explanation: In PHP binary operators, only the | will keep the longest string length and pad with NULs. So we XOR string B with itself, leading to a string with NUL bytes of the same length as B, then OR that with A. This will pad A with NUL bytes and use the length of B, if B is longer than A. We do the same with B, and only then XOR.


  • arrow function variant
  • missed the requirement of outputting the longest string

Vyxal, 4 bytes


Try it Online!

Because Vyxal auto-pads lol.


C     # convert each character in each string to it's ordinal value
 ÷꘍   # bitwise xor each item
   C  # and convert the result back to characters

Alternatively, using Keg mode:

Vyxal K, 1 byte

Try it Online!


Charcoal, 33 bytes


Try it online! Link is to verbose version of code. Takes input as an array of two strings. Explanation:


Loop over the longer length of the strings.


Loop over the strings.


Save the result of the previous loop, if any.


Get the ordinal at the current index, if that is less than the current string.


Emulate bitwise XOR by subtracting the bitwise AND from the bitwise OR, then convert back to a character.


Python 2, 80 72 69 bytes

lambda*a:''.join(map(lambda x,y:chr(ord(x or'\0')^ord(y or'\0')),*a))

Try it online!

Uneven lengths are annoying...


PowerShell, 86 81 bytes


Try it online!

-5 bytes thanks to @mazzy

  • \$\begingroup\$ Welcome. You can use $args[1]|% t*y instead [char[]]($args[1]). The % t*y means "call the method toCharArray. And you can omit $i=0. See also Tips for golfing in PowerShell \$\endgroup\$
    – mazzy
    Jul 19, 2020 at 10:44
  • \$\begingroup\$ Am I wrong or does this not work for two strings of unequal length? \$\endgroup\$ Jul 26, 2020 at 15:51
  • \$\begingroup\$ @Thomas yep. You can see the two arguments are different length. \$\endgroup\$
    – Wasif
    Jul 26, 2020 at 15:54
  • \$\begingroup\$ 1. You have to quote your input, if it contains spaces. You are actually processing I and am instead of I am fat and You are thin. 2. As you can see in your result, it contains only 1 byte instead of two bytes (am has two bytes), because you process up to the length of the first string. In conclusion, you just omit bytes if the first string is the shorter one and your code crashes if the second string is shorter. \$\endgroup\$ Jul 26, 2020 at 16:10

C (gcc),  44  43 bytes


Try it online!

Uses recursion, note that to print strings containing the null byte one will have to manage the strings as arrays. (See the footer of the link for an example)

C (gcc),  50  49 bytes


Try it online!

Slightly safer version (doesn't read past end of strings, requires enough memory to exist past them though - a la strcpy).

C (gcc),  61  60 bytes


Try it online!

As safe as any standard C function taking a buffer, at the cost of a few more bytes.

-1 byte from each thanks to ceilingcat!

  • \$\begingroup\$ You're relying on the first string storing as much memory as the second string in case the second string is longer than the first. That's not really playing fair. Certainly not how strings work in C. \$\endgroup\$
    – Noodle9
    Jul 20, 2020 at 1:15
  • 1
    \$\begingroup\$ You're also relying on memory past the end of the shorter string to be initialised to zero. \$\endgroup\$
    – Noodle9
    Jul 20, 2020 at 1:23
  • \$\begingroup\$ I agree it isn't the fairest, but given how strings are represented in C there isn't a much cleaner way to write this as a function. Your solution doesn't provide any means by which to access the new string, other than trapping the output stream. The only truly portable way would be to take 6 parameters: a, len(a), b, len(b), buffer, len(buffer). But defensive coding has never really been the goal of these submissions ;) \$\endgroup\$
    – LambdaBeta
    Jul 20, 2020 at 20:27
  • \$\begingroup\$ There are many ways to make your code cleaner. Not reading past the end of strings for starters. The point is that work is being done outside of your function to make it work. To change mine to return a string is trivial and certainly doesn't involve passing string lengths. Simply malloc a string buffer based on the maximum strlens of the inputs and return that pointer after creating the result there. \$\endgroup\$
    – Noodle9
    Jul 20, 2020 at 21:38
  • \$\begingroup\$ same can be done in mine. it also still requires work outside of the function in terms of free. A solution would be something like this: Try it online! \$\endgroup\$
    – LambdaBeta
    Jul 20, 2020 at 22:32

Stax, 7 bytes


Run and debug it


Husk, 11 bytes


Try it online!

Unfortunately Husk doesn't have a bitwise XOR command (that I could find), so we need to do: arg1 OR (v) arg2 minus arg1 AND (n) arg2, costing an extra 5 bytes...


x86 machine code, 29 bytes

Machine code:

00000034: 31 d2 e3 08 8a 11 41 41 84 d2 e1 fe ac 84 c0 75  1.....AA.......u
00000044: 06 e3 09 89 ce 31 c9 30 d0 aa eb e4 c3           .....1.0.....

Commented assembly:

        .intel_syntax noprefix
        .globl .strxor
        // Doesn't follow standard calling convention.
        // Input:
        //      EDI: Output buffer large enough for longest string
        //      ESI, ECX: Null terminated strings to XOR
        // Output:
        //      NON-null terminated string stored in EDI.
        // Always clear EDX.
        xor     edx, edx
        // We mark the end of the shorter string
        // by setting ECX to null.
        jecxz   .Lskip_ecx
        // Read a byte from ECX into DL.
        mov     dl, byte ptr [ecx]
        // Increment twice because LOOPZ decrements.
        inc     ecx
        inc     ecx
        // Was it '\0'?
        test    dl, dl
        // If so, set ECX to NULL by using LOOPZ in place.
        // CMOVZ ECX, EAX also works, but needs P6.
        // This works with i386 and is the same size.
        loopz   .Lclear_ecx
        // Load from ESI and autoincrement
        lods    al, byte ptr [esi]
        // Test for '\0'
        test    al, al
        jnz     .Lno_swap
.Lswap: // '\0' found
        // If ECX is null, we are at the end of both strings.
        jecxz   .Lend
        // Otherwise, we swap ESI and ECX, and then clear ECX.
        // Set ESI to ECX.
        mov     esi, ecx
        // And set ECX to NULL.
        xor     ecx, ecx
        // fallthrough
        // XOR the two bytes together
        xor     al, dl
        // Store to EDI and autoincrement
        stos    byte ptr [edi], al
        // Loop unconditionally.
        jmp     .Lloop

Try it online!

The code reads two null terminated strings from esi and ecx, and stores the NON null terminated string in edi.

The logic is probably easier to see in the equivalent C code.

void strxor(char *dst, const char *src1, const char *src2)
    for (;;) {
        char x = '\0';
        if (src2 != NULL) {
            x = *src2++;
            if (x == '\0') { // eos
                src2 = NULL;
        char y = *src1++;
        if (y == '\0') {
            if (src2 == NULL) { // both eos
            } else { // src2 is longer
                src1 = src2;
                src2 = NULL;
        *dst++ = x ^ y;

I don't null terminate because null bytes naturally show up in the output and it saves a byte.

I may also add a version with explicit lengths which accepts null bytes, but the solution would be significantly less elegant.


Perl 5, 14 bytes

sub f{pop^pop}

Try it online!


Scala 3, 30 29 bytes

a=>b=>a zipAll(b,0,0)map(_^_)

Try it online

Scala 2, 41 39 38 bytes

Used infix notation to turn .map into just map

a=>b=>a zipAll(b,0,0)map(a=>a._1^a._2)

Inputs and output are lists of integers

Try it online


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.