左移 (<<)
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.
左移操作符 (<<
) 将第一个操作数向左移动指定位数,左边超出的位数将会被清除,右边将会补零。
尝试一下
const a = 5; // 00000000000000000000000000000101
const b = 2; // 00000000000000000000000000000010
console.log(a << b); // 00000000000000000000000000010100
// Expected output: 20
语法
a << b
描述
左移操作符将第一个操作数向左移动指定位数,左边超出的位数将会被清除,右边将会补零。
例如, 9 << 2
得出 36:
js
9 (十进制): 00000000000000000000000000001001 (二进制)
--------------------------------
9 << 2 (十进制): 00000000000000000000000000100100 (二进制) = 36 (十进制)
移动任意数字 x
至左边 y
位,得出 x * 2 ** y
。
所以例如:9 << 3
等价于 9 * 2³ = 9 * 8 = 72
。
示例
使用左移
js
9 << 3; // 72
// 9 * 2³ = 9 * 8 = 72
规范
Specification |
---|
ECMAScript® 2025 Language Specification # sec-left-shift-operator |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Bitwise left 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.