Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
count_of_set_bits.cpp File Reference

Implementation to [count number of set bits of a number] (https://www.geeksforgeeks.org/count-set-bits-in-an-integer/) in an integer. More...

#include <cassert>
#include <iostream>
Include dependency graph for count_of_set_bits.cpp:

Namespaces

namespace  bit_manipulation
 for IO operations
 
namespace  count_of_set_bits
 Functions for the count sets bits implementation.
 

Functions

std::uint64_t bit_manipulation::count_of_set_bits::countSetBits (std ::int64_t n)
 The main function implements set bit count.
 
static void test ()
 
int main ()
 Main function.
 

Detailed Description

Implementation to [count number of set bits of a number] (https://www.geeksforgeeks.org/count-set-bits-in-an-integer/) 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

Function Documentation

◆ countSetBits()

std::uint64_t bit_manipulation::count_of_set_bits::countSetBits ( std ::int64_t n)

The main function implements set bit count.

Parameters
nis the number whose set bit will be counted
Returns
total number of set-bits in the binary representation of number n
38 { // int64_t is preferred over int so that
39 // no Overflow can be there.
40
41 int count = 0; // "count" variable is used to count number of set-bits('1')
42 // in binary representation of number 'n'
43 while (n != 0) {
44 ++count;
45 n = (n & (n - 1));
46 }
47 return count;
48 // Why this algorithm is better than the standard one?
49 // Because this algorithm runs the same number of times as the number of
50 // set-bits in it. Means if my number is having "3" set bits, then this
51 // while loop will run only "3" times!!
52}
Here is the call graph for this function:

◆ main()

int main ( void )

Main function.

Returns
0 on exit
79 {
80 test(); // run self-test implementations
81 return 0;
82}

◆ test()

static void test ( )
static
56 {
57 // n = 4 return 1
58 assert(bit_manipulation::count_of_set_bits::countSetBits(4) == 1);
59 // n = 6 return 2
60 assert(bit_manipulation::count_of_set_bits::countSetBits(6) == 2);
61 // n = 13 return 3
62 assert(bit_manipulation::count_of_set_bits::countSetBits(13) == 3);
63 // n = 9 return 2
64 assert(bit_manipulation::count_of_set_bits::countSetBits(9) == 2);
65 // n = 15 return 4
66 assert(bit_manipulation::count_of_set_bits::countSetBits(15) == 4);
67 // n = 25 return 3
68 assert(bit_manipulation::count_of_set_bits::countSetBits(25) == 3);
69 // n = 97 return 3
70 assert(bit_manipulation::count_of_set_bits::countSetBits(97) == 3);
71 // n = 31 return 5
72 assert(bit_manipulation::count_of_set_bits::countSetBits(31) == 5);
73 std::cout << "All test cases successfully passed!" << std::endl;
74}
T endl(T... args)