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.

静的な Atomics.or() メソッドは、配列内の指定した位置の値に指定した値でビット単位の OR を計算し、その位置にあった古い値を返します。これは不可分操作で、修正された値が書き戻されるまで、他の書き込みが起こらないことを保証します。

試してみましょう

// 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

構文

js
Atomics.or(typedArray, index, value);

引数

typedArray

整数の型付き配列です。 Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array, BigInt64Array, BigUint64Array の何れかです。

index

typedArray でビット単位の OR を計算する位置です。

value

ビット単位の OR を取る数値です。

返値

指定された位置 (typedArray[index]) にあった古い値です。

例外

  • typedArray が許可された整数型の何れでもない場合、TypeError が発生します。
  • indextypedArray の範囲を超えている場合、 RangeError が発生します。

解説

ビット単位の OR 操作は、 a と b のどちらかが 1 であった場合に 1 を生成します。 OR 操作の真理値表を示します。

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

例えば、ビット単位の OR を 5 | 1 で行うと、結果は 0101 すなわち 10 進数で 5 となります。

5  0101
1  0001
   ----
5  0101

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

仕様書

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

ブラウザーの互換性

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

関連情報