|
Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
|
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>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. More... | |
| static void | test () |
| Self-test implementations. More... | |
| int | main () |
| Main function. More... | |
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)
| 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.
| A | is the given number whose bits will be flipped to get number B |
| B | is the given target number |
| int main | ( | void | ) |
|
static |
Self-test implementations.