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()
57 assert(bit_manipulation::count_of_set_bits::countSetBits(4) == 1);
59 assert(bit_manipulation::count_of_set_bits::countSetBits(6) == 2);
61 assert(bit_manipulation::count_of_set_bits::countSetBits(13) == 3);
63 assert(bit_manipulation::count_of_set_bits::countSetBits(9) == 2);
65 assert(bit_manipulation::count_of_set_bits::countSetBits(15) == 4);
67 assert(bit_manipulation::count_of_set_bits::countSetBits(25) == 3);
69 assert(bit_manipulation::count_of_set_bits::countSetBits(97) == 3);
71 assert(bit_manipulation::count_of_set_bits::countSetBits(31) == 5);