This page was translated from English by the community.Learn more and join the MDN Web Docs community.
Atomics.xor()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2021년 12월.
Atomics.xor() 정적 메서드는 배열에서 주어진 위치에 주어진 값으로 XOR 비트 연산을 수행하고해당 포지션의 기존 값을 반환합니다. 이 아토믹 연산은 수정된 값이 쓰이기 전까지 다른 쓰기 연산이 일어나지 않음을 보장합니다.
In this article
시도해 보기
// Create a SharedArrayBuffer with a size in bytesconst buffer = new SharedArrayBuffer(16);const uint8 = new Uint8Array(buffer);uint8[0] = 7;// 7 (0111) XOR 2 (0010) = 5 (0101)console.log(Atomics.xor(uint8, 0, 2));// Expected output: 7console.log(Atomics.load(uint8, 0));// Expected output: 5구문
js
Atomics.xor(typedArray, index, value)매개 변수
typedArray정수형 형식화 배열.
Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,BigInt64Array,BigUint64Array중 하나.index비트 연산 XOR를 계산할
typedArray의 인덱스입니다.value비트 연산 XOR와 같이 계산할 숫자입니다.
반환 값
주어진 위치(typedArray[index])의 예전 값.
예외
typedArray가 허용된 정수형이 아닐 경우TypeError가 발생합니다.index가typedArray의 범위를 벗어날 경우RangeError가 발생합니다.
설명
비트 연산 XOR은a와b 이 다르면 1을 산출합니다. XOR 연산에 대한 진리표는 다음과 같습니다.
a | b | a ^ b |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
예를 들어,5 ^ 1의 비트 연산 XOR의 값은0100이며 10진수로 4입니다.
5 01011 0001 ----4 0100
예제
>xor 사용하기
js
const sab = new SharedArrayBuffer(1024);const ta = new Uint8Array(sab);ta[0] = 5;Atomics.xor(ta, 0, 1); // 이전 값인 5를 반환합니다Atomics.load(ta, 0); // 4명세
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-atomics.xor> |