Skip to content

Instantly share code, notes, and snippets.

@Ch-sriram
Created May 28, 2020 14:25
Show Gist options
  • Save Ch-sriram/212f8185414fcb71278c6028128fcb2a to your computer and use it in GitHub Desktop.
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.
// 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