Implementation to count number of set bits of a number in an integer.
More...
#include <cassert>
#include <iostream>
Implementation to count number of set bits of a number in an integer.
We are given an integer number. We need to calculate the number of set bits in it.
A binary number consists of two digits. They are 0 & 1. Digit 1 is known as set bit in computer terms. Worst Case Time Complexity: O(log n) Space complexity: O(1)
- Author
- Swastika Gupta
-
Prashant Thakur
◆ countSetBits()
| std::uint64_t bit_manipulation::count_of_set_bits::countSetBits |
( |
std ::int64_t |
n | ) |
|
The main function implements set bit count.
- Parameters
-
| n | is the number whose set bit will be counted |
- Returns
- total number of set-bits in the binary representation of number
n
◆ main()
Main function.
- Returns
- 0 on exit
◆ test()
58 assert(bit_manipulation::count_of_set_bits::countSetBits(4) == 1);
60 assert(bit_manipulation::count_of_set_bits::countSetBits(6) == 2);
62 assert(bit_manipulation::count_of_set_bits::countSetBits(13) == 3);
64 assert(bit_manipulation::count_of_set_bits::countSetBits(9) == 2);
66 assert(bit_manipulation::count_of_set_bits::countSetBits(15) == 4);
68 assert(bit_manipulation::count_of_set_bits::countSetBits(25) == 3);
70 assert(bit_manipulation::count_of_set_bits::countSetBits(97) == 3);
72 assert(bit_manipulation::count_of_set_bits::countSetBits(31) == 5);