ビット排他的論理和 (^)
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
ビット排他的論理和演算子 (^
) は、両方のオペランドの対応するビットの一方だけが 1
である位置のビットで 1
を返します。
試してみましょう
const a = 5; // 00000000000000000000000000000101
const b = 3; // 00000000000000000000000000000011
console.log(a ^ b); // 00000000000000000000000000000110
// Expected output: 6
構文
js
a ^ b;
解説
オペランドは 32 ビットの整数値に変換され、ビット (ゼロまたは 1) の並びによって表現されます。32 ビットを超える数値は最上位のビットが破棄されます。例えば、次の 32 ビットを超える整数は 32 ビット整数に変換されます。
変換前: 11100110111110100000000000000110000000000001 変換後: 10100000000000000110000000000001
第 1 オペランドの各ビットは、第 2 オペランドの対応するビットと組みになります。第 1 ビットは第 1 ビットへ、第 2 ビットは第 2 ビットへ、という具合にです。
この演算子は各ビットの組み合わせに適用され、結果はビット単位で構築されます。
XOR 演算の真理値表は次のようになります。
a | b | a XOR b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
9 (10 進数) = 00000000000000000000000000001001 (2 進数) 14 (10 進数) = 00000000000000000000000000001110 (2 進数) -------------------------------- 14 ^ 9 (10 進数) = 00000000000000000000000000000111 (2 進数) = 7 (10 進数)
ある数 x
と 0
のビット排他的論理和は x
になります。
例
ビット排他的論理和の使用
js
// 9 (00000000000000000000000000001001)
// 14 (00000000000000000000000000001110)
14 ^ 9;
// 7 (00000000000000000000000000000111)
仕様書
Specification |
---|
ECMAScript® 2025 Language Specification # prod-BitwiseXORExpression |
ブラウザーの互換性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Bitwise XOR ( a ^ b ) |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.