Atomics.or()
静的な Atomics
.or()
メソッドは、配列内の指定した位置の値に指定した値でビット単位の OR を計算し、その位置にあった古い値を返します。これは不可分操作で、修正された値が書き戻されるまで、他の書き込みが起こらないことを保証します。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
構文
Atomics.and(typedArray, index, value)
引数
typedArray
- 整数の型付き配列です。
Int8Array
,Uint8Array
,Int16Array
,Uint16Array
,Int32Array
,Uint32Array
,BigInt64Array
,BigUint64Array
の何れかです。 index
typedArray
でビット単位の OR を計算する位置です。value
- ビット単位の OR を取る数値です。
返値
指定された位置にあった古い値です (typedArray[index]
)。
例外
typedArray
が許可された整数型の何れでもない場合、TypeError
が発生します。index
がtypedArray
の範囲を超えている場合、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 の使用
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
仕様書
ブラウザーの互換性
BCD tables only load in the browser