ビット論理和 (|)

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); // 00000000000000000000000000000111
// Expected output: 7

構文

js
a | b;

解説

オペランドは 32 ビットの整数値に変換され、ビット (ゼロまたは 1) の並びによって表現されます。32 ビットを超える数値は最上位のビットが破棄されます。例えば、次の 32 ビットを超える整数は 32 ビット整数に変換されます。

変換前: 11100110111110100000000000000110000000000001
変換後:             10100000000000000110000000000001

第 1 オペランドの各ビットは、第 2 オペランドの対応するビットと組みになります。第 1 ビット第 1 ビットへ、第 2 ビット第 2 ビットへ、という具合にです。

この演算子は各ビットの組み合わせに適用され、結果はビット単位で構築されます。

OR 演算の真理値表は次のようになります。

a b a OR b
0 0 0
0 1 1
1 0 1
1 1 1
js
.    9 (10 進数) = 00000000000000000000000000001001 (2 進数)
    14 (10 進数) = 00000000000000000000000000001110 (2 進数)
                   --------------------------------
14 | 9 (10 進数) = 00000000000000000000000000001111 (2 進数) = 15 (10 進数)

ある数 x0 のビット論理和は x になります。

ビット論理和の使用

js
// 9  (00000000000000000000000000001001)
// 14 (00000000000000000000000000001110)

14 | 9;
// 15 (00000000000000000000000000001111)

仕様書

Specification
ECMAScript® 2025 Language Specification
# prod-BitwiseORExpression

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
Bitwise OR (a | b)

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報