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

Implementation to [Count number of bits to be flipped to convert A to B] (https://www.geeksforgeeks.org/count-number-of-bits-to-be-flipped-to-convert-a-to-b/) in an integer. More...

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

Namespaces

namespace  bit_manipulation
 for IO operations
 
namespace  count_bits_flip
 Functions for the count bits flip implementation.
 

Functions

std::uint64_t bit_manipulation::count_bits_flip::countBitsFlip (std::int64_t A, std::int64_t B)
 The main function implements count of bits flip required.
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Implementation to [Count number of bits to be flipped to convert A to B] (https://www.geeksforgeeks.org/count-number-of-bits-to-be-flipped-to-convert-a-to-b/) in an integer.

We are given two numbers A and B. Our task is to count the number of bits needed to be flipped to convert A to B.

Explanation:

A = 01010 B = 10100 As we can see, the bits of A that need to be flipped are 01010. If we flipthese bits, we get 10100, which is B.

Worst Case Time Complexity: O(log n) Space complexity: O(1)

Author
Yash Raj Singh

Function Documentation

◆ countBitsFlip()

std::uint64_t bit_manipulation::count_bits_flip::countBitsFlip ( std::int64_t A,
std::int64_t B )

The main function implements count of bits flip required.

Parameters
Ais the given number whose bits will be flipped to get number B
Bis the given target number
Returns
total number of bits needed to be flipped to convert A to B
44 { // int64_t is preferred over int so that
45 // no Overflow can be there.
46
47 int count =
48 0; // "count" variable is used to count number of bits flip of the
49 // number A to form B in binary representation of number 'n'
50 A = A ^ B;
51 while (A) {
52 A = A & (A - 1);
53 count++;
54 }
55 return count;
56}
Here is the call graph for this function:

◆ main()

int main ( void )

Main function.

Returns
0 on exit
85 {
86 test(); // run self-test implementations
87 return 0;
88}
static void test()
Self-test implementations.
Definition count_bits_flip.cpp:64
Here is the call graph for this function:

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void
64 {
65 // A = 10, B = 20 return 4
66 assert(bit_manipulation::count_bits_flip::countBitsFlip(10, 20) == 4);
67 // A = 20, B = 25 return 3
68 assert(bit_manipulation::count_bits_flip::countBitsFlip(20, 25) == 3);
69 // A = 7, B = 10 return 3
70 assert(bit_manipulation::count_bits_flip::countBitsFlip(7, 10) == 3);
71 // A = 17, B = 25 return 1
72 assert(bit_manipulation::count_bits_flip::countBitsFlip(17, 25) == 1);
73 // A = 11, B = 8 return 2
74 assert(bit_manipulation::count_bits_flip::countBitsFlip(11, 8) == 2);
75 // A = 21, B = 22 return 2
76 assert(bit_manipulation::count_bits_flip::countBitsFlip(21, 22) == 2);
77 // A = 7, B = 786 return 5
78 assert(bit_manipulation::count_bits_flip::countBitsFlip(7, 786) == 5);
79 std::cout << "All test cases successfully passed!" << std::endl;
80}
T endl(T... args)
Here is the call graph for this function: