Atomics.xor()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since December 2021.
Die statische Methode Atomics.xor()
führt eine bitweise XOR-Operation mit einem angegebenen Wert an einer gegebenen Position in dem Array durch und gibt den alten Wert an dieser Position zurück. Diese atomare Operation garantiert, dass keine weiteren Schreibvorgänge erfolgen, bis der geänderte Wert zurückgeschrieben wurde.
Probieren Sie es aus
// Create a SharedArrayBuffer with a size in bytes
const buffer = new SharedArrayBuffer(16);
const uint8 = new Uint8Array(buffer);
uint8[0] = 7;
// 7 (0111) XOR 2 (0010) = 5 (0101)
console.log(Atomics.xor(uint8, 0, 2));
// Expected output: 7
console.log(Atomics.load(uint8, 0));
// Expected output: 5
Syntax
Atomics.xor(typedArray, index, value)
Parameter
typedArray
-
Ein Integer-Typed-Array. Eines von
Int8Array
,Uint8Array
,Int16Array
,Uint16Array
,Int32Array
,Uint32Array
,BigInt64Array
oderBigUint64Array
. index
-
Die Position im
typedArray
, an der das bitweise XOR berechnet wird. value
-
Die Zahl, mit der das bitweise XOR berechnet wird.
Rückgabewert
Der alte Wert an der gegebenen Position (typedArray[index]
).
Ausnahmen
TypeError
-
Wird ausgelöst, wenn
typedArray
nicht einer der zulässigen Integer-Typen ist. RangeError
-
Wird ausgelöst, wenn
index
außerhalb der Grenzen destypedArray
liegt.
Beschreibung
Die bitweise XOR-Operation ergibt 1, wenn a
und b
unterschiedlich sind. Die Wahrheitstabelle für die XOR-Operation lautet:
a |
b |
a ^ b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Zum Beispiel ergibt ein bitweises XOR von 5 ^ 1
das Ergebnis 0100
, welches im Dezimalsystem 4 ist.
5 0101 1 0001 ---- 4 0100
Beispiele
Verwendung von xor
const sab = new SharedArrayBuffer(1024);
const ta = new Uint8Array(sab);
ta[0] = 5;
Atomics.xor(ta, 0, 1); // returns 5, the old value
Atomics.load(ta, 0); // 4
Spezifikationen
Specification |
---|
ECMAScript® 2025 Language Specification # sec-atomics.xor |
Browser-Kompatibilität
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
xor |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support