Find the total number of bits needed to be flipped
Find the total number of bits needed to be flipped to convert a given integer to another.
For example,
x = 65 (which is 01000001 in binary)
y = 80 (which is 01010000 in binary)
Output: The total number of bits to be flipped is 2
The idea is to take XOR of the given two integers. After calculating the XOR, the problem will reduce to counting set bits in the XOR output.
Following is the C++, Java, and Python implementation of the idea:
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include <iostream> #include <bitset> using namespace std; // Find the total number of bits needed to be flipped to convert `x` to `y` int findBits(int x, int y) { // take XOR of `x` and `y` and store in `n` int n = x ^ y; // Using Brian Kernighan’s algorithm to count set bits // `count` stores the total bits set in `n` int count = 0; while (n) { n = n & (n - 1); // clear the least significant bit set count++; } return count; } int main() { int x = 65; int y = 80; cout << x << " in binary is " << bitset<8>(x) << endl; cout << y << " in binary is " << bitset<8>(y) << endl; cout << "\nThe total number of bits to be flipped is " << findBits(x, y); return 0; } |
Output:
65 in binary is 01000001
80 in binary is 01010000
The total number of bits to be flipped is 2
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
class Main { // Find the total number of bits needed to be flipped to convert `x` to `y` public static int findBits(int x, int y) { // take XOR of `x` and `y` and store in `n` int n = x ^ y; // Using Brian Kernighan’s algorithm to count set bits // `count` stores the total bits set in `n` int count = 0; while (n != 0) { n = n & (n - 1); // clear the least significant bit set count++; } return count; } public static void main(String[] args) { int x = 65; int y = 80; System.out.println(x + " in binary is " + Integer.toBinaryString(x)); System.out.println(y + " in binary is " + Integer.toBinaryString(y)); System.out.println("\nThe total number of bits to be flipped is " + findBits(x, y)); } } |
Output:
65 in binary is 1000001
80 in binary is 1010000
The total number of bits to be flipped is 2
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# Find the total number of bits needed to be flipped to convert `x` to `y` def findBits(x, y): # take XOR of `x` and `y` and store in `n` n = x ^ y # Using Brian Kernighan’s algorithm to count set bits # `count` stores the total bits set in `n` count = 0 while n: n = n & (n - 1) # clear the least significant bit set count = count + 1 return count if __name__ == '__main__': x = 65 y = 80 print(f'{x} in binary is {bin(x)}') print(f'{y} in binary is {bin(y)}') print('\nThe total number of bits to be flipped is', findBits(x, y)) |
Output:
65 in binary is 0b1000001
80 in binary is 0b1010000
The total number of bits to be flipped is 2
Suggested Read: https://graphics.stanford.edu/~seander/bithacks.html
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)