RunMat
  • Pricing
RunMat
GitHub
GitHub
DownloadSign InTry in Browser
DesktopRuntimeServer
RunMat

Run math blazing fast

GitHubX (Twitter)LinkedIn

Company

  • About
  • Pricing
  • Contact

Explore

  • RunMat for academia
  • RunMat vs MATLAB Online
  • Benchmarks

Get product updates and release notes from the RunMat team.

© 2026 Dystr · Made withfor the scientific community.

RunMat™ is a registered trademark of Dystr, Inc. MATLAB® is a registered trademark of The MathWorks, Inc. RunMat is not affiliated with, endorsed by, or sponsored by The MathWorks, Inc.

LicensePrivacy
/
See all docs
Builtin Reference
    • and
    • bitand
    • bitcmp
    • bitget
    • bitor
    • bitset
    • bitshift
    • bitxor
    • not
    • or
    • xor

xor — Compute element-wise logical XOR across scalar or array inputs with MATLAB-compatible expansion.

xor(A, B) returns logical true where exactly one operand is nonzero, using MATLAB-compatible implicit expansion and logical conversion rules.

Syntax

tf = xor(A, B)

Inputs

NameTypeRequiredDefaultDescription
AAnyYes—Left operand.
BAnyYes—Right operand.

Returns

NameTypeDescription
tfLogicalArrayLogical element-wise exclusive disjunction result.

Errors

IdentifierWhenMessage
RunMat:xor:InvalidInputAn input is not logical, numeric, complex, character, or gpuArray with gatherable numeric data.xor: unsupported input type; expected logical, numeric, complex, or character data
RunMat:xor:SizeMismatchInput shapes are not broadcast-compatible.xor: array sizes are not compatible for broadcasting

How xor works

  • MATLAB-compatible mode accepts logical and real numeric arrays, including every integer class with exact zero/nonzero truth semantics. RunMat compatibility mode additionally accepts complex and character arrays.
  • Supports MATLAB-style implicit expansion so scalars and singleton dimensions broadcast automatically.
  • Propagates empty dimensions: if a broadcasted axis has length 0, the output is an empty logical array with the same shape.
  • Treats NaN values as true, matching MATLAB's element-wise logical semantics.
  • Uses a validated floating logical_xor hook when available. Native integer handles bypass floating hooks and gather exactly; automatic residency may return to host, while explicit gpuArray inputs restore a logical gpuArray result.

Does RunMat run xor on the GPU?

When both operands reside on the GPU and the active provider implements the logical_xor hook, RunMat lowers the call into a device kernel that writes 0 or 1 for each element. The fusion planner treats xor as an element-wise operation, so fused expressions (for example, xor(A > 0, B)) stay on device without intermediate gathers. The native auto-offload planner also recognizes the builtin's elementwise tag, promoting mixed CPU/GPU operands to the active device when it is profitable. If the provider lacks the hook, the runtime gathers the inputs to host memory automatically and executes the CPU implementation instead of failing.

GPU memory and residency

You usually do not need to call gpuArray explicitly. RunMat's native auto-offload logic moves data to the GPU when a fused expression benefits from device execution, and results stay resident until you gather them or the planner needs host access. Call gpuArray only when you want to seed residency manually, or when you are porting MATLAB code that already uses explicit device transfers.

Examples

Checking if exactly one condition is true

result = xor(true, false)

Expected output:

result =
  logical
     1

Performing XOR on numeric arrays element-wise

A = [1 0 2 0];
B = [3 4 0 0];
C = xor(A, B)

Expected output:

C =
  1×4 logical array
     0     1     1     0

Applying XOR with implicit expansion against a scalar

mask = [1; 0; 3; 0];
flag = xor(mask, 1)

Expected output:

flag =
  4×1 logical array
     0
     1
     0
     1

Comparing character arrays with XOR

lhs = ['R' 'u' 0];
rhs = ['R' 0 'n'];
diff = xor(lhs, rhs)

Expected output:

diff =
  1×3 logical array
     0     1     1

Running xor directly on gpuArray inputs

G1 = gpuArray([0 2 0 4]);
G2 = gpuArray([1 0 3 4]);
deviceResult = xor(G1, G2);
hostResult = gather(deviceResult)

Expected output:

deviceResult =
  1×4 gpuArray logical array
     1     1     1     0
hostResult =
  1×4 logical array
     1     1     1     0

Using xor with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how xor changes the result.

Run a small xor example, explain the result, then change one input and compare the output.

FAQ

Does xor return logical values?⌄

Yes. The result is a logical scalar (true/false) when the broadcasted shape contains exactly one element; otherwise the function returns a logical array. On the GPU the kernel writes 0.0/1.0 elements, and the runtime converts them back to logical values when you gather.

How are NaN values handled?⌄

NaN counts as true. For example, xor(NaN, 5) returns false because both operands evaluate to non-zero, whereas xor(NaN, 0) returns true.

Is implicit expansion supported?⌄

Yes. The inputs follow MATLAB-style implicit expansion rules: dimensions of length 1 broadcast across the other input. Fully incompatible shapes raise a size-mismatch error.

Can I use xor with complex numbers?⌄

Only in RunMat compatibility mode. Complex input is a classified RunMat extension using nonzero-component truth semantics; MATLAB-compatible mode rejects it before dispatch. Character input is gated the same way.

What happens when only one input is a gpuArray?⌄

The runtime may use provider execution when compatible. Otherwise it gathers transparently and computes exactly on the host; explicit gpuArray intent restores the logical result to the owning provider, while automatic residency may remain on host.

What does xor do in MATLAB?⌄

xor(A, B) returns logical true where exactly one of A or B is nonzero, and false where both are zero or both are nonzero. The result is always a logical array.

How is xor different from or in MATLAB?⌄

or(A, B) (i.e. A | B) returns true when either or both inputs are nonzero. xor(A, B) returns true only when exactly one input is nonzero — it is false when both are true.

Can I use xor with more than two inputs?⌄

xor accepts exactly two inputs. To XOR multiple arrays, chain calls: xor(xor(A, B), C). This computes the parity (true when an odd number of inputs are true).

Does xor support GPU arrays in RunMat?⌄

Yes. xor runs on the GPU with elementwise fusion and implicit expansion. Both inputs can be GPU arrays or one can be a scalar.

Related Logical functions

Bit

and · bitand · bitcmp · bitget · bitor · bitset · bitshift · bitxor · not · or

Tests

allfinite · isfinite · isgpuarray · isinf · islogical · isnan · isnumeric · isreal · issparse

Rel

eq · ge · gt · isequal · isequaln · le · lt · ne

Logical

logical

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how xor is executed, line by line, in Rust.

  • View the source for xor in Rust on GitHub
  • Learn how the RunMat runtime works
  • Found a bug? Open an issue with a minimal reproduction.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.

Download RunMatOpen Sandbox
On this page
  • Syntax
  • Inputs
  • Returns
  • Errors
  • How xor works
  • Does RunMat run xor on the GPU?
  • GPU memory and residency
  • Examples
  • Checking if exactly one condition is true
  • Performing XOR on numeric arrays element-wise
  • Applying XOR with implicit expansion against a scalar
  • Comparing character arrays with XOR
  • Running xor directly on gpuArray inputs
  • Using xor with coding agents
  • FAQ
  • Related Logical functions
  • Bit
  • Tests
  • Rel
  • Logical
  • Open-source implementation
  • About RunMat