右シフト代入演算子 (>>=)
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 つのオペランドで右シフトを実行し、その結果を左オペランドに代入します。
試してみましょう
let a = 5; // 00000000000000000000000000000101
a >>= 2; // 00000000000000000000000000000001
console.log(a);
// 予想される結果: 1
let b = -5; // 11111111111111111111111111111011
b >>= 2; // 11111111111111111111111111111110
console.log(b);
// 予想される結果: -2
構文
js
x >>= y
解説
x >>= y
は x = x >> y
と同等ですが、式 x
が一度だけ評価される点が異なります。
例
右シフト代入の使用
js
let a = 5; // (00000000000000000000000000000101)
a >>= 2; // 1 (00000000000000000000000000000001)
let b = -5; // (-00000000000000000000000000000101)
b >>= 2; // -2 (-00000000000000000000000000000010)
let c = 5n;
c >>= 2n; // 1n
仕様書
Specification |
---|
ECMAScript® 2026 Language Specification # sec-assignment-operators |
ブラウザーの互換性
Loading…