1302. Count Leading Zeros of an Integer

EasyBit ManipulationBit ManipulationLeading ZerosWidth

Given a non-negative integer n and a width bits, return the number of leading 0-bits in the bits-wide representation of n. The classic 32-bit count is the special case bits = 32. For n = 0 the answer is bits.

Input: A JSON object {"n": <integer>, "bits": <width>} with 1 <= bits <= 32 and 0 <= n < 2^bits.

Output: Return the number of leading zeros within the given width.

Examples

Example 1
Input: {"n":16,"bits":8}
Output: 3
Explanation: In 8 bits, 00010000 has three leading zeros.
Example 2
Input: {"n":1,"bits":32}
Output: 31
Explanation: In 32 bits, only the lowest bit is set -> 31 leading zeros.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →