← Back to challenges

Recursion: Right Shift by Division

JavaScriptHardrecursionnumbersbit_operations

Instructions

The right shift operation is similar to floor division by powers of two, thus, the process is repetitive and can be done recursively.

Sample calculation using the right shift operator ( >> ):

80 >> 3 = floor(80/2^3) = floor(80/8) = 10
-24 >> 2 = floor(-24/2^2) = floor(-24/4) = -6
-5 >> 1 = floor(-5/2^1) = floor(-5/2) = -3

Write a function that mimics (without the use of >>) the right shift operator and returns the result from the two given integers.

Examples

shiftToRight(80, 3) ➞ 10

shiftToRight(-24, 2) ➞ -6

shiftToRight(-5, 1) ➞ -3

shiftToRight(4666, 6) ➞ 72

shiftToRight(3777, 6) ➞ 59

shiftToRight(-512, 10) ➞ -1

Notes

  • There will be no negative values for the second parameter y.
  • This challenge is more like recreating of the right shift operation, thus, the use of the operator directly is prohibited.
  • You are expected to solve this challenge via recursion.
  • A non-recursive version of this challenge can be found in here.
  • If you think recursion if fun, here is a [collection] (https://innokodakademija.com/collection/YJH4pAyqDbhLc4STc) of such challenges.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.