ビット排他的論理和代入演算子 (^=)
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
ビット排他的論理和代入演算子 (^=
) は、 2 つのオペランドに対してビット単位のXOR を実行し、その結果を左側のオペランドに代入します。
試してみましょう
let a = 5; // 00000000000000000000000000000101
a ^= 3; // 00000000000000000000000000000011
console.log(a); // 00000000000000000000000000000110
// 予想される結果: 6
構文
js
x ^= y
解説
x ^= y
は x = x ^ y
と同等ですが、式 x
は一度だけ評価されます。
例
>ビット排他的論理和代入の使用
js
let a = 5; // (00000000000000000000000000000101)
a ^= 3; // (00000000000000000000000000000011)
console.log(a); // 6 (00000000000000000000000000000110)
let b = 5; // (00000000000000000000000000000101)
b ^= 0; // (00000000000000000000000000000000)
console.log(b); // 5 (00000000000000000000000000000101)
let c = 5n;
c ^= 3n;
console.log(c); // 6n
仕様書
Specification |
---|
ECMAScript® 2026 Language Specification> # sec-assignment-operators> |
ブラウザーの互換性
Loading…