Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is not arithmetic shift better in countSetBits.js? #1152

Open
oThinas opened this issue Jul 19, 2024 · 0 comments · May be fixed by #1155
Open

Is not arithmetic shift better in countSetBits.js? #1152

oThinas opened this issue Jul 19, 2024 · 0 comments · May be fixed by #1155

Comments

@oThinas
Copy link

oThinas commented Jul 19, 2024

The file countSetBits.js has the function:

/**
 * @param {number} originalNumber
 * @return {number}
 */
export default function countSetBits(originalNumber) {
  let setBitsCount = 0;
  let number = originalNumber;

  while (number) {
    // Add last bit of the number to the sum of set bits.
    setBitsCount += number & 1;

    // Shift number right by one bit to investigate other bits.
    number >>>= 1;
  }

  return setBitsCount;
}

I wonder if it wouldn't be better to use an arithmetic shift on line number >>>= 1 instead of a logical shift, thus preserving the sign of the number.

@hitaishi-goel hitaishi-goel linked a pull request Jul 20, 2024 that will close this issue
Parvezkhan0 added a commit to Parvezkhan0/Aldrometa-Security that referenced this issue Aug 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant