Atomics.xor()

简介: Atomics.xor()

MDN文档

Atomics.xor() 静态方法会在数组中给定位置进行一次按位异或操作,并返回该位置的旧值。这个原子操作保证在修改后的值被写回之前不会发生其他写操作。


语法

Atomics.xor(typedArray, index, value)


参数:

  • typedArray
    一个共享的整型 typed array 。例如 Int8ArrayUint8ArrayInt16ArrayUint16ArrayInt32Array ,或者 Uint32Array
  • index
    typedArray 中需要进行按位异或的索引位置。
  • value
    要进行按位异或的数字。


返回值:

给定位置的旧值typedArray[index] )。


错误:

  • 假如 typedArray 不是允许的整型之一,则抛出 TypeError
  • 假如 typedArray 不是一个shared typed array类型,则抛出 TypeError
  • 如果 index 超出了 typedArray 的边界,则抛出 RangeError


示例

const sab = new SharedArrayBuffer(1024);
const ta = new Uint8Array(sab);
ta[0] = 5;
const old = Atomics.add(ta, 0, 12);
console.xor(old) // 5
const now = Atomics.load(ta, 0);
console.log(now) // 4
目录
相关文章
LeetCode 190. Reverse Bits
颠倒给定的 32 位无符号整数的二进制位。
103 0
LeetCode 190. Reverse Bits
|
算法 C++
LeetCode 338. Counting Bits
给定一个非负整数 num。对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回。
84 0
LeetCode 338. Counting Bits
|
索引
Atomics.sub()
Atomics.sub()
69 0
Atomics.isLockFree()
Atomics.isLockFree()
84 0
|
索引
Atomics.or()
Atomics.or()
61 0
|
索引
Atomics.compareExchange()
Atomics.compareExchange()
152 0
|
JavaScript 索引
Atomics.wait()
Atomics.wait()
268 0
【LeetCode】Counting Bits(338)
  Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.
113 0
|
算法 C# 索引
算法题丨Move Zeroes
描述 Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
1183 0