Logo

Programming-Idioms

  • C++
  • Haskell
  • 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.
using System.Linq;
var c = a.Zip(b, (l, r) => (byte)(l ^ r)).ToArray();

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