左移 (<<)

左移操作符 (<<) 将第一个操作数向左移动指定位数,左边超出的位数将会被清除,右边将会补零。

语法

a << b

描述

左移操作符将第一个操作数向左移动指定位数,左边超出的位数将会被清除,右边将会补零。

例如, 9 << 2 得出 36:

.    9 (十进制): 00000000000000000000000000001001 (二进制)
                  --------------------------------
9 << 2 (十进制): 00000000000000000000000000100100 (二进制) = 36 (十进制)

移动任意数字 x 至左边 y 位,得出 x * 2 ** y 。
所以例如:9 << 3 等价于 9 * 2³ = 9 * 8 = 72

例子

使用左移

9 << 3; // 72

// 9 * 2³ = 9 * 8 = 72

规范

Specification
ECMAScript (ECMA-262)
Bitwise Shift Operators

浏览器兼容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
Bitwise left shift (a << b)Chrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support 3Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support 0.1.100

Legend

Full support  
Full support

参见