-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add Kernighan's Algorithm for Counting Set Bits #1107
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
Open
Open
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b749625
fixes #1106
smitgabani a6aebb4
add CountSetBits algorithm
smitgabani ba6f851
add CountSetBits algorithm
smitgabani d0ba2a3
add CountSetBits algorithm & test
smitgabani 474cc9a
Merge branch 'TheAlgorithms:master' into master
smitgabani aa4dd7c
Merge branch 'master' of https://github.com/smitgabani/JavaScript
smitgabani d90363b
fix
smitgabani 9ec4f5f
fix
smitgabani fe9555b
fix
smitgabani c0da047
Merge branch 'TheAlgorithms:master' into master
smitgabani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
fixes #1106
- Loading branch information
commit b74962555af1df5457306c385bf0eed1a259d6db
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // author: Smit Gabani | ||
| // https://github.com/smitgabani | ||
|
|
||
| /** | ||
| * explanation link: https://iq.opengenus.org/brian-kernighan-algorithm/ | ||
| * The below algorithm is called as Brian Kernighan's algorithm | ||
smitgabani marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * @param: num takes number whose number of set bit is to be found | ||
| * @return: the count of set bits in the binary equivalent | ||
| * */ | ||
| function CountSetBits(num) { | ||
| 'use strict' | ||
smitgabani marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let cnt = 0; | ||
| while(num > 0) { | ||
| cnt++; | ||
| // num = num & (num - 1); | ||
smitgabani marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| num&=(num-1); | ||
| } | ||
| return cnt; | ||
| } | ||
|
|
||
| // console.log(CountSetBits(10)) | ||
smitgabani marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // expected output: 2 | ||
|
|
||
| export { CountSetBits } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.