multiplyByTwo

Easy · Bit Manipulation · 3 test cases

Multiply an integer by 2 using only bitwise operators.

Examples

multiplyByTwo(5) → 10
multiplyByTwo(-8) → -16

Starter code

function multiplyByTwo(n) {

}

Complexity of the optimal solution

Time O(1), space O(1). The left shift `<<` operator shifts all bits of a number to the left by a specified amount, effectively multiplying it by a power of 2. A shift by 1 multiplies by 2. This is a single, constant time CPU instruction.

Solve multiplyByTwo in the browser