feat: Modified count_set_bits.cpp (#1634)

* Modified: Replaced existing code with faster implementation

* Changed long long to "int64_t"

* Indentation Fixed.

* Modified Documentation.

* Updated authors of count_set_bits.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* Apply suggestions from code review

Co-authored-by: David Leal <halfpacho@gmail.com>

* Added proper indentation in "main" function

Co-authored-by: David Leal <halfpacho@gmail.com>

Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
Prashant Thakur 2021-10-17 06:27:23 +05:30 committed by GitHub
parent 27f1ed312f
commit 6272b2af08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,26 +1,21 @@
/** /**
* @file * @file
* @brief Implementation to [count sets * @brief Implementation to [count number of set bits of a number]
* bits](https://www.geeksforgeeks.org/count-set-bits-in-an-integer/) in an * (https://www.geeksforgeeks.org/count-set-bits-in-an-integer/) in an
* integer. * integer.
* *
* @details * @details
* We are given an integer number. Lets say, number. The task is to first * We are given an integer number. We need to calculate the number of set bits in it.
* calculate the binary digit of a number and then calculate the total set bits
* of a number.
* *
* Set bits in a binary number is represented by 1. Whenever we calculate the * A binary number consists of two digits. They are 0 & 1. Digit 1 is known as
* binary number of an integer value it is formed as the combination of 0s and * set bit in computer terms.
* 1s. So digit 1 is known as a set bit in computer terms. * Worst Case Time Complexity: O(log n)
* Time Complexity: O(log n)
* Space complexity: O(1) * Space complexity: O(1)
* @author [Swastika Gupta](https://github.com/Swastyy) * @author [Swastika Gupta](https://github.com/Swastyy)
* @author [Prashant Thakur](https://github.com/prashant-th18)
*/ */
#include <cassert> /// for assert #include <cassert> /// for assert
#include <iostream> /// for io operations #include <iostream> /// for IO operations
#include <vector> /// for std::vector
/** /**
* @namespace bit_manipulation * @namespace bit_manipulation
* @brief Bit manipulation algorithms * @brief Bit manipulation algorithms
@ -36,24 +31,27 @@ namespace count_of_set_bits {
/** /**
* @brief The main function implements set bit count * @brief The main function implements set bit count
* @param n is the number whose set bit will be counted * @param n is the number whose set bit will be counted
* @returns the count of the number set bit in the binary representation of `n` * @returns total number of set-bits in the binary representation of number `n`
*/ */
std::uint64_t countSetBits(int n) { std::uint64_t countSetBits(std :: int64_t n) { // int64_t is preferred over int so that
int count = 0; // "count" variable is used to count number of 1's in binary // no Overflow can be there.
// representation of the number
while (n != 0) { int count = 0; // "count" variable is used to count number of set-bits('1') in
count += n & 1; // binary representation of number 'n'
n = n >> 1; // n=n/2 while (n != 0)
{
++count;
n = (n & (n - 1));
} }
return count; return count;
// Why this algorithm is better than the standard one?
// Because this algorithm runs the same number of times as the number of
// set-bits in it. Means if my number is having "3" set bits, then this while loop
// will run only "3" times!!
} }
} // namespace count_of_set_bits } // namespace count_of_set_bits
} // namespace bit_manipulation } // namespace bit_manipulation
/**
* @brief Self-test implementations
* @returns void
*/
static void test() { static void test() {
// n = 4 return 1 // n = 4 return 1
assert(bit_manipulation::count_of_set_bits::countSetBits(4) == 1); assert(bit_manipulation::count_of_set_bits::countSetBits(4) == 1);
@ -67,9 +65,12 @@ static void test() {
assert(bit_manipulation::count_of_set_bits::countSetBits(15) == 4); assert(bit_manipulation::count_of_set_bits::countSetBits(15) == 4);
// n = 25 return 3 // n = 25 return 3
assert(bit_manipulation::count_of_set_bits::countSetBits(25) == 3); assert(bit_manipulation::count_of_set_bits::countSetBits(25) == 3);
// n = 97 return 3
assert(bit_manipulation::count_of_set_bits::countSetBits(97) == 3);
// n = 31 return 5
assert(bit_manipulation::count_of_set_bits::countSetBits(31) == 5);
std::cout << "All test cases successfully passed!" << std::endl; std::cout << "All test cases successfully passed!" << std::endl;
} }
/** /**
* @brief Main function * @brief Main function
* @returns 0 on exit * @returns 0 on exit