From 5d3b238b908300a0a8e8ea6145dc5a89dd6d4f70 Mon Sep 17 00:00:00 2001 From: Rudra2637 Date: Sun, 12 Oct 2025 11:44:38 +0530 Subject: [PATCH] fix: update countBitsFlip to use uint64_t for parameters and counter --- bit_manipulation/count_bits_flip.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/bit_manipulation/count_bits_flip.cpp b/bit_manipulation/count_bits_flip.cpp index 2ab2ce31c15..e4be0558973 100644 --- a/bit_manipulation/count_bits_flip.cpp +++ b/bit_manipulation/count_bits_flip.cpp @@ -41,13 +41,20 @@ namespace count_bits_flip { * @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. + std::uint64_t A, + std::uint64_t B) { // uint64_t is preferred over int so that + // no Overflow can be there. + //It's preferred over int64_t because it Guarantees that inputs are always non-negative, + //which matches the algorithmic problem statement. + //set bit counting is conceptually defined only for non-negative numbers. + //Provides a type Safety: Using an unsigned type helps prevent accidental negative values, - int count = + std::uint64_t 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' + //Count is uint64_t because it Prevents theoretical overflow if someone passes very large integers. + // Behavior stays the same for all normal inputs. + // Safer for edge cases. A = A ^ B; while (A) { A = A & (A - 1);