Atomics.or()

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.or() berechnet ein bitweises OR mit einem angegebenen Wert an einer bestimmten Position im Array und gibt den alten Wert an dieser Position zurück. Diese atomare Operation garantiert, dass keine andere Schreiboperation durchgeführt wird, 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] = 5;

// 5 (0101) OR 2 (0010) = 7 (0111)
console.log(Atomics.or(uint8, 0, 2));
// Expected output: 5

console.log(Atomics.load(uint8, 0));
// Expected output: 7

Syntax

js
Atomics.or(typedArray, index, value)

Parameter

typedArray

Ein ganzzahliges typisiertes Array. Eines von Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array, BigInt64Array oder BigUint64Array.

index

Die Position im typedArray, an der das bitweise OR berechnet wird.

value

Die Zahl, mit der das bitweise OR berechnet wird.

Rückgabewert

Der alte Wert an der angegebenen Position (typedArray[index]).

Ausnahmen

TypeError

Wird ausgelöst, wenn typedArray nicht einer der zulässigen Ganzzahltypen ist.

RangeError

Wird ausgelöst, wenn index außerhalb der Grenzen des typedArray liegt.

Beschreibung

Die bitweise OR-Operation liefert 1, wenn entweder a oder b den Wert 1 haben. Die Wahrheitstabelle für die OR-Operation lautet:

a b a | b
0 0 0
0 1 1
1 0 1
1 1 1

Zum Beispiel ergibt ein bitweises OR von 5 | 1 0101, was in Dezimal 5 entspricht.

5  0101
1  0001
   ----
5  0101

Beispiele

Verwendung von or

js
const sab = new SharedArrayBuffer(1024);
const ta = new Uint8Array(sab);
ta[0] = 2;

Atomics.or(ta, 0, 1); // returns 2, the old value
Atomics.load(ta, 0); // 3

Spezifikationen

Specification
ECMAScript® 2025 Language Specification
# sec-atomics.or

Browser-Kompatibilität

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
or

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Siehe auch