Bit Shifts for Fast Powers of Two

Medium ⏱ 12 min 58% acceptance ★★★★☆ 4.3
Write a function double_n_times(value, n) that returns value doubled n times, implemented using the left shift operator << instead of repeated multiplication by 2 or **.

Examples

Example 1
Input
double_n_times(3, 4)
Output
48
Explanation

3 << 4 = 3 * 16 = 48.

Example 2
Input
double_n_times(1, 0)
Output
1
Explanation

Shifting by 0 leaves the value unchanged.

Constraints

  • value is a non-negative integer; n is a non-negative integer.

Topics

Operatorsbitwise shift

Companies

IntelCisco

Hints

Hint 1

`value << n` is equivalent to `value * (2 ** n)` for non-negative n.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.