Atomics.or()
Atomics.or() 靜態方法在一個數組的指定位置上,計算一個給定值的按位或(bitwise 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中計算按位或的位置。 value-
用於計算按位或(bitwise OR)的數字。
返回值
給定位置(typedArray[index])上的舊值。
異常
TypeError-
如果
typedArray不是允許的整數型別之一,則丟擲。 RangeError-
如果
index在typedArray中超出界限,則丟擲。
描述
按位或操作,如果 a 或 b 為 1,則結果為 1。OR 操作的真值表如下:
a |
b |
a | b |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
例如,按位或操作 5 | 1 的結果是 0101,十進位制為 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
規範
| 規範 |
|---|
| ECMAScript® 2026 語言規範 # sec-atomics.or |
瀏覽器相容性
載入中…