Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Js

Idiom #238 Xor byte arrays

Write in a new byte array c the xor result of byte arrays a and b.

a and b have the same size.

const c = Uint8Array.from(a, (v, i) => v ^ b[i])
#include <array>
#include <cstddef>
std::array<std::byte, a.size()> c;
for (auto ia = a.begin(), ib = b.begin(); auto & rc : c) {
  rc = *ia++ ^ *ib++;
}

std::byte xor operator requires C++17.
range for init-statement  requires C++20.

New implementation...
< >
programming-idioms.org