Back to all solutions

#3125 - Maximum Number That Makes Result of Bitwise AND Zero

Problem Description

Given an integer n, return the maximum integer x such that x <= n, and the bitwise AND of all the numbers in the range [x, n] is 0.

Solution

/**
 * @param {number} n
 * @return {number}
 */
var maxNumber = function(n) {
  let bit = 1;

  while (bit <= n) {
    bit *= 2;
  }

  return bit / 2 - 1;
};