Second-Rightmost Zero Bit

Description


Implement the missing code, denoted by ellipses. You may not modify the pre-existing code.

Presented with the integer n, find the 0-based position of the second rightmost zero bit in its binary representation (it is guaranteed that such a bit exists), counting from right to left.

Return the value of 2position_of_the_found_bit.

Example

For n = 37, the output should be secondRightmostZeroBit(n) = 8.

3710 = 1001012. The second rightmost zero bit is at position 3 (0-based) from the right in the binary representation of n. Thus, the answer is 23 = 8.

Input/Output

  • [execution time limit] 4 seconds (js)

  • [input] integer n

    Guaranteed constraints:
    4 \leq n \leq 2^{30}.

  • [output] integer

[JavaScript (ES6)] Syntax Tips

1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
console.log("This prints to the console when you Run Tests");
return "Hello, " + name;
}

Solution


1
2
3
4
5
6
7
8
function secondRightmostZeroBit(n) {
  return (
    1 <<
    (n.toString(2).length -
      n.toString(2).lastIndexOf("0", n.toString(2).lastIndexOf("0") - 1) -
      1)
  );
}