mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-05-07 13:53:16 +08:00
clang-format and clang-tidy fixes for d0dc7eb3
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Program to Count set bits in an integer
|
||||
* @brief Program to Count set bits in an integer
|
||||
*
|
||||
* @details
|
||||
* Time Complexity :- O(log n)
|
||||
* Space complexity :- O(1)
|
||||
* @author [Swastika Gupta](https://github.com/swastyy)
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <iostream> /// for io operations
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <iostream> /// for io operations
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
/**
|
||||
* @namespace bit_manipulation
|
||||
* @brief bit set counting algorithm
|
||||
@@ -29,10 +29,11 @@ namespace bitCount {
|
||||
*/
|
||||
|
||||
std::uint64_t countSetBits(int n) {
|
||||
int count = 0; // "count" variable is used to count number of 1's in binary representation of the number
|
||||
while (n!=0) {
|
||||
int count = 0; // "count" variable is used to count number of 1's in binary
|
||||
// representation of the number
|
||||
while (n != 0) {
|
||||
count += n & 1;
|
||||
n=n>>1; // n=n/2
|
||||
n = n >> 1; // n=n/2
|
||||
}
|
||||
return count;
|
||||
}
|
||||
@@ -45,17 +46,17 @@ std::uint64_t countSetBits(int n) {
|
||||
*/
|
||||
static void test() {
|
||||
// n = 4 return 1
|
||||
assert(bit_manipulation::bitCount::countSetBits(4)==1);
|
||||
assert(bit_manipulation::bitCount::countSetBits(4) == 1);
|
||||
// n = 6 return 2
|
||||
assert(bit_manipulation::bitCount::countSetBits(6)==2);
|
||||
assert(bit_manipulation::bitCount::countSetBits(6) == 2);
|
||||
// n = 13 return 3
|
||||
assert(bit_manipulation::bitCount::countSetBits(13)==3);
|
||||
assert(bit_manipulation::bitCount::countSetBits(13) == 3);
|
||||
// n = 9 return 2
|
||||
assert(bit_manipulation::bitCount::countSetBits(9)==2);
|
||||
assert(bit_manipulation::bitCount::countSetBits(9) == 2);
|
||||
// n = 15 return 4
|
||||
assert(bit_manipulation::bitCount::countSetBits(15)==4);
|
||||
assert(bit_manipulation::bitCount::countSetBits(15) == 4);
|
||||
// n = 25 return 3
|
||||
assert(bit_manipulation::bitCount::countSetBits(25)==3);
|
||||
assert(bit_manipulation::bitCount::countSetBits(25) == 3);
|
||||
std::cout << "All test cases passed" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user