右シフト (>>)
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つ目のオペランドを指定されたビット数だけ右にずらします。右にずらしてあふれたビットは廃棄されます。最も左のビットをコピーしながらずれて入ります。最も左のビットが以前の最も左のビットと同じになるため、符号ビット (最も左のビット) は変化しません。よって「符号維持」という名前です。
試してみましょう
const a = 5; // 00000000000000000000000000000101
const b = 2; // 00000000000000000000000000000010
const c = -5; // 11111111111111111111111111111011
console.log(a >> b); // 00000000000000000000000000000001
// Expected output: 1
console.log(c >> b); // 11111111111111111111111111111110
// Expected output: -2
構文
js
a >> b;
解説
この演算子は、1 つ目のオペランドを指定されたビット数だけ右にずらします。右にずらしてあふれたビットは廃棄されます。最も左のビットをコピーしながらずれて入ります。最も左のビットが以前の最も左のビットと同じになるため、符号ビット (最も左のビット) は変化しません。よって「符号維持」という名前です。
例えば、 9 >> 2
は 2 となります。
. 9 (10 進数): 00000000000000000000000000001001 (2 進数) -------------------------------- 9 >> 2 (10 進数): 00000000000000000000000000000010 (2 進数) = 2 (10 進数)
同様に、 -9 >> 2
は符号が保存されるため、 -3
になります。
. -9 (10 進数): 11111111111111111111111111110111 (2 進数) -------------------------------- -9 >> 2 (10 進数): 11111111111111111111111111111101 (2 進数) = -3 (10 進数)
例
右シフトの使用
js
9 >> 2; // 2
-9 >> 2; // -3
仕様書
Specification |
---|
ECMAScript® 2025 Language Specification # sec-signed-right-shift-operator |
ブラウザーの互換性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Bitwise right shift ( 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.