Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

left-shift
javascript
bitwise

What is the bitwise left-shift (<<) operator in JavaScipt?

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The left-shift operator or the bitwise left-shift operator (<<) in JavaScript shits each bit of a binary to the left by n positions. The zeros of the bits are shifted from the right to the left and the leftmost bits will fall off.

For instance, 2 << 1 tells JavaScript to shift the bits of 2 to the left by 1. Hence, 2, which is 0010, will become 0100.

Syntax

number << noOfShifts
The syntax for left-shift operator in Ruby

Parameters

number: This is the number we want to shift the bits of.

noOfShifts: This is the number of times we want to left-shift the bits of the number number.

Return value

It returns an integer that is the decimal equivalent of the binary gotten after the shifting.

Example

// shift the binary bits of some numbers
console.log(2 << 1)
console.log(10 << 2)
console.log(900 << 2)
console.log(5 << 3)
console.log(200 << 10)
Applying left shift operator to numbers

Explanation

  • Lines 2–6: We use the bitwise left shift operator to shift the bits of some numbers. Next, we log the results to the console.

RELATED TAGS

left-shift
javascript
bitwise

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring