Created
May 28, 2020 14:25
-
-
Save Ch-sriram/212f8185414fcb71278c6028128fcb2a to your computer and use it in GitHub Desktop.
Given a Number - N, count the number of set bits in N.
This file contains 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
// Refer checkith() function here: https://gist.github.com/Ch-sriram/fef3d0a6274d20f6705495b7cf5b22e4 | |
bool checkith (long long N, int i) { | |
return N & ((long long) 1 << i); | |
} | |
int countSetBits (long long N) { | |
int count = 0; | |
for (int i = 0; i < 64; ++i) | |
if (checkith(N, i)) | |
++count; | |
return count; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment