DEV Community

Discussion on: Bit difference

Collapse
 
lexlohr profile image
Alex Lohr

In JS, we can use

const countBitsFlip = (a, b) => {
  let c = 0;
  for (let n = a ^ b; n !== 0; c++) {
    n = n & (n - 1);
  }
  return c;
}
Enter fullscreen mode Exit fullscreen mode