TheAlgorithms/C++ 1.0.0
All the 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 <cstdint>
#include <iostream>
Go to the source code of this file.
Namespaces | |
namespace | bit_manipulation |
for assert | |
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. | |
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)
Definition in file count_bits_flip.cpp.
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 |
Definition at line 43 of file count_bits_flip.cpp.
int main | ( | void | ) |
Main function.
Definition at line 86 of file count_bits_flip.cpp.
|
static |
Self-test implementations.
Definition at line 65 of file count_bits_flip.cpp.