Is not arithmetic shift better in countSetBits.js?
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 38/100
- Issue type
- Refactor
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- javascript
- Domain
- tooling
Research direction
Start in countSetBits.js at the shift on the line using >>>= 1. Read the surrounding function and any nearby tests or documentation to determine the intended behavior for negative numbers. Done means the shift behavior is explicitly settled and the implementation and relevant tests consistently reflect that decision.
Written by the indexing model from the issue text.
Description
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.
- Dominant language
- JavaScript
- Stars
- 197k
- Forks
- 31k
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from trekhleb/javascript-algorithms
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
trekhleb/javascript-algorithms#2102 · 1 comment ·
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
trekhleb/javascript-algorithms#2085 · 6 comments · 1 reaction ·
-
LinkedList methods. Open
Difficulty 3/5 1-2 days Newbie friendliness 35/100
trekhleb/javascript-algorithms#2065 · 1 comment ·
-
Difficulty 3/5 1-2 days Newbie friendliness 55/100
trekhleb/javascript-algorithms#2057 · 1 comment ·
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
trekhleb/javascript-algorithms#2056 ·
All issues in trekhleb/javascript-algorithms
Similar issues
-
code-quality refactoring
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
github/gh-aw-firewall#8816 ·
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
langchain-ai/deepagents#6450 ·
-
optimization optimization:agents-md-curator
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
githubnext/gh-aw-cao#13143 ·
-
status: needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100