1286. Divide a Number by 2 Using Right Shift

EasyBit ManipulationBit ManipulationShiftArithmetic

Given a non-negative integer n and a non-negative exponent k, compute the floor of n divided by 2^k using a right shift instead of division. The classic 'divide by 2' is the special case k = 1.

Input: A JSON object {"n": <non-negative integer>, "k": <exponent>} with 0 <= k <= 20.

Output: Return floor(n / 2^k) as an integer.

Examples

Example 1
Input: {"n":20,"k":1}
Output: 10
Explanation: 20 >> 1 = 10 = 20 / 2.
Example 2
Input: {"n":100,"k":3}
Output: 12
Explanation: 100 >> 3 = 12 = floor(100 / 8).

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →