From 53469897523a151eb4c88ce9848a661371e69edf Mon Sep 17 00:00:00 2001 From: YourGitHubUsername Date: Wed, 12 Mar 2025 00:27:38 +0530 Subject: [PATCH 1/2] updated code --- bit_manipulation/count_bits_flip.cpp | 89 ++++++++++------------------ 1 file changed, 30 insertions(+), 59 deletions(-) diff --git a/bit_manipulation/count_bits_flip.cpp b/bit_manipulation/count_bits_flip.cpp index 2ab2ce31c15..f6e6f069684 100644 --- a/bit_manipulation/count_bits_flip.cpp +++ b/bit_manipulation/count_bits_flip.cpp @@ -22,68 +22,39 @@ #include /// for assert #include #include /// for IO operations -/** - * @namespace bit_manipulation - * @brief Bit manipulation algorithms - */ -namespace bit_manipulation { -/** - * @namespace count_bits_flip - * @brief Functions for the [count bits - * flip](https://www.geeksforgeeks.org/count-set-bits-in-an-integer/) - * implementation - */ -namespace count_bits_flip { -/** - * @brief The main function implements count of bits flip required - * @param A is the given number whose bits will be flipped to get number B - * @param B is the given target number - * @returns total number of bits needed to be flipped to convert A to B - */ -std::uint64_t countBitsFlip( - std::int64_t A, - std::int64_t B) { // int64_t is preferred over int so that - // no Overflow can be there. +#include - int count = - 0; // "count" variable is used to count number of bits flip of the - // number A to form B in binary representation of number 'n' - A = A ^ B; - while (A) { - A = A & (A - 1); - count++; +class Solution { +public: + int minBitFlips(int start, int goal) { + int xor_value = start ^ goal; // Step 1: Find XOR of start and goal + int res = 0; + + // Step 2: Count number of 1s in XOR result + while (xor_value > 0) { + res += xor_value & 1; // Increment count if last bit is 1 + xor_value >>= 1; // Right shift to check next bit + } + + return res; // Return total bit flips required } - return count; -} -} // namespace count_bits_flip -} // namespace bit_manipulation +}; -/** - * @brief Self-test implementations - * @returns void - */ -static void test() { - // A = 10, B = 20 return 4 - assert(bit_manipulation::count_bits_flip::countBitsFlip(10, 20) == 4); - // A = 20, B = 25 return 3 - assert(bit_manipulation::count_bits_flip::countBitsFlip(20, 25) == 3); - // A = 7, B = 10 return 3 - assert(bit_manipulation::count_bits_flip::countBitsFlip(7, 10) == 3); - // A = 17, B = 25 return 1 - assert(bit_manipulation::count_bits_flip::countBitsFlip(17, 25) == 1); - // A = 11, B = 8 return 2 - assert(bit_manipulation::count_bits_flip::countBitsFlip(11, 8) == 2); - // A = 21, B = 22 return 2 - assert(bit_manipulation::count_bits_flip::countBitsFlip(21, 22) == 2); - // A = 7, B = 786 return 5 - assert(bit_manipulation::count_bits_flip::countBitsFlip(7, 786) == 5); - std::cout << "All test cases successfully passed!" << std::endl; -} -/** - * @brief Main function - * @returns 0 on exit - */ int main() { - test(); // run self-test implementations + Solution solution; + int start, goal; + + // Taking user input + std::cout << "Enter start value: "; + std::cin >> start; + std::cout << "Enter goal value: "; + std::cin >> goal; + + // Calculating minimum bit flips + int result = solution.minBitFlips(start, goal); + + // Displaying the result + std::cout << "Minimum bit flips required: " << result << std::endl; + return 0; } From eb5aaf91e3bdf53524a66dd1ba41b1afddbf6d51 Mon Sep 17 00:00:00 2001 From: YourGitHubUsername Date: Wed, 12 Mar 2025 00:43:29 +0530 Subject: [PATCH 2/2] final update --- bit_manipulation/count_bits_flip.cpp | 92 ++++++++++++++++++---------- 1 file changed, 61 insertions(+), 31 deletions(-) diff --git a/bit_manipulation/count_bits_flip.cpp b/bit_manipulation/count_bits_flip.cpp index f6e6f069684..bad0399e1d3 100644 --- a/bit_manipulation/count_bits_flip.cpp +++ b/bit_manipulation/count_bits_flip.cpp @@ -22,39 +22,69 @@ #include /// for assert #include #include /// for IO operations -#include +/** + * @namespace bit_manipulation + * @brief Bit manipulation algorithms + */ +namespace bit_manipulation { +/** + * @namespace count_bits_flip + * @brief Functions for the [count bits + * flip](https://www.geeksforgeeks.org/count-set-bits-in-an-integer/) + * implementation + */ +namespace count_bits_flip { +/** + * @brief The main function implements count of bits flip required + * @param A is the given number whose bits will be flipped to get number B + * @param B is the given target number + * @returns total number of bits needed to be flipped to convert A to B + */ +std::uint64_t countBitsFlip( + std::int64_t A, + std::int64_t B) { // int64_t is preferred over int so that + // no Overflow can be there. -class Solution { -public: - int minBitFlips(int start, int goal) { - int xor_value = start ^ goal; // Step 1: Find XOR of start and goal - int res = 0; - - // Step 2: Count number of 1s in XOR result - while (xor_value > 0) { - res += xor_value & 1; // Increment count if last bit is 1 - xor_value >>= 1; // Right shift to check next bit - } - - return res; // Return total bit flips required - } -}; + int xor_value = A ^ B; // Step 1: Find XOR of start and goal + int res = 0; + + // Step 2: Count number of 1s in XOR result + while (xor_value > 0) { + res += xor_value & 1; // Increment count if last bit is 1 + xor_value >>= 1; // Right shift to check next bit + } + + return res; +} +} // namespace count_bits_flip +} // namespace bit_manipulation +/** + * @brief Self-test implementations + * @returns void + */ +static void test() { + // A = 10, B = 20 return 4 + assert(bit_manipulation::count_bits_flip::countBitsFlip(10, 20) == 4); + // A = 20, B = 25 return 3 + assert(bit_manipulation::count_bits_flip::countBitsFlip(20, 25) == 3); + // A = 7, B = 10 return 3 + assert(bit_manipulation::count_bits_flip::countBitsFlip(7, 10) == 3); + // A = 17, B = 25 return 1 + assert(bit_manipulation::count_bits_flip::countBitsFlip(17, 25) == 1); + // A = 11, B = 8 return 2 + assert(bit_manipulation::count_bits_flip::countBitsFlip(11, 8) == 2); + // A = 21, B = 22 return 2 + assert(bit_manipulation::count_bits_flip::countBitsFlip(21, 22) == 2); + // A = 7, B = 786 return 5 + assert(bit_manipulation::count_bits_flip::countBitsFlip(7, 786) == 5); + std::cout << "All test cases successfully passed!" << std::endl; +} +/** + * @brief Main function + * @returns 0 on exit + */ int main() { - Solution solution; - int start, goal; - - // Taking user input - std::cout << "Enter start value: "; - std::cin >> start; - std::cout << "Enter goal value: "; - std::cin >> goal; - - // Calculating minimum bit flips - int result = solution.minBitFlips(start, goal); - - // Displaying the result - std::cout << "Minimum bit flips required: " << result << std::endl; - + test(); // run self-test implementations return 0; }