Last active
January 4, 2023 11:21
-
-
Save icecoobe/0b5a8eef63f3e04cf0f621ef968df542 to your computer and use it in GitHub Desktop.
count 1 bits
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
#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