Skip to content

Instantly share code, notes, and snippets.

@icecoobe
Last active January 4, 2023 11:21
Show Gist options
  • Save icecoobe/0b5a8eef63f3e04cf0f621ef968df542 to your computer and use it in GitHub Desktop.
Save icecoobe/0b5a8eef63f3e04cf0f621ef968df542 to your computer and use it in GitHub Desktop.
count 1 bits
#include <iostream>
size_t count_one_bit(uint64_t num)
{
size_t count = 0x0;
while (num > 0x0)
{
if ((num & 0x01) != 0)
{
count++;
}
num >>= 0x01;
}
return count;
}
int main()
{
std::cout << count_one_bit(0xFF) << std::endl;
std::cout << count_one_bit(1) << std::endl;
std::cout << count_one_bit(56) << std::endl;
std::cout << count_one_bit(10) << std::endl;
std::cout << count_one_bit(100) << std::endl;
std::cout << count_one_bit(0b11010101010) << std::endl;
std::cout << count_one_bit(0b11010111010) << std::endl;
std::cout << "-----------------------------" << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment