Easy

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 2^position_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 2^3 = 8.

Input/Output

  • [execution time limit] 0.5 seconds (cpp)

  • [input] integer n

    Guaranteed constraints:
    4 ≤ n ≤ 2^30.

  • [output] integer

[C++] Syntax Tips

// Prints help message to the console
// Returns a string
std::string helloWorld(std::string name) {
    std::cout << "This prints to the console when you Run Tests" << std::endl;
    return "Hello, " + name;
}

더보기

Solution

int secondRightmostZeroBit(int n) {
  return ~(n|~n&(n+1))&((n|~n&(n+1))+1);
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Count Sum of Two Representations 2  (0) 2020.04.12
<Codesignal> Least Factorial  (0) 2020.04.12
<Codesignal> Mirror Bits  (0) 2020.04.09
<Codesignal> Range Bit Count  (0) 2020.04.07
<Codesignal> Array Packing  (0) 2020.04.07

+ Recent posts