W3cubDocs

/JavaScript

Left shift

The << shifts the first operand the specified number of bits to the left. Excess bits shifted off to the left are discarded. Zero bits are shifted in from the right.

Syntax

a << b

Description

This operator shifts the first operand the specified number of bits to the left. Excess bits shifted off to the left are discarded. Zero bits are shifted in from the right.

For example, 9 << 2 yields 36:

.    9 (base 10): 00000000000000000000000000001001 (base 2)
                  --------------------------------
9 << 2 (base 10): 00000000000000000000000000100100 (base 2) = 36 (base 10)

Bitwise shifting any number x to the left by y bits yields x * 2 ** y.
So e.g.: 9 << 3 translates to: 9 * (2 ** 3) = 9 * (8) = 72.

Examples

Using left shift

9 << 3; // 72

// 9 * (2 ** 3) = 9 * (8) = 72

Specifications

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Bitwise left shift (a << b) 1 12 1 3 3 1
Mobile
Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
Bitwise left shift (a << b) 1 18 4 10.1 1 1.0
Server
Node.js
Bitwise left shift (a << b) 0.1.100

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Left_shift