Equal Pair Of Bits

Description


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

You’re given two integers, n and m. Find position of the rightmost pair of equal bits in their binary representations (it is guaranteed that such a pair exists), counting from right to left.

Return the value of 2position_of_the_found_pair (0-based).

Example

For n = 10 and m = 11, the output should be equalPairOfBits(n, m) = 2.

1010 = 10102, 1110 = 10112, the position of the rightmost pair of equal bits is the bit at position 1 (0-based) from the right in the binary representations. So the answer is 21 = 2.

Input/Output

  • [execution time limit] 4 seconds (js)

  • [input] integer n

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

  • [input] integer m

    Guaranteed constraints:
    0 \leq m \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
9
10
11
12
function equalPairOfBits(n, m) {
  return (
    1 <<
    (
      (n ^ m)
        .toString(2)
        .split("")
        .reverse()
        .join("") + "0"
    ).indexOf("0")
  );
}