count Number Of Bit Flips Algorithm

If a bit is present at a point otherwise dedicated to a parity bit but is not used for parity, it may be referred to as a mark parity bit if the parity bit is always 1, or a space parity bit if the bit is always 0.For a given set of bits, if the count of bits with a value of 1 is even, the parity bit value is put to 1 make the total count of 1s in the whole set (including the parity bit) an odd number. parity was also used on at least some paper-tape (punched tape) data entry systems (which preceded magnetic tape systems).Parity in this form, apply across multiple parallel signals, is known as a transverse redundancy check. In a parallel bus, there is one longitudinal redundancy check bit per parallel signal.
/**
 *  Given two numbers A and B, count number of flips required to convert number A to B
 *  Approach : Take XOR of A and B, the result will contain set bits only at positions
 *  where A differed B. Therefore number of set bits in the result of A ^ B is the number
 *  of flips required to convert A to B
 */

#include <iostream>

int countSetBits( int x )
{
  int count = 0;
  while( x ) {
    ++count;
    x = x & (x - 1);
  }
  return count;
}

int countBitFlips( int a, int b )
{
  return countSetBits(a ^ b);
}

int main()
{
  int x, y;
  std::cout << "Enter number 1 :";
  std::cin >> x;
  std::cout << "Enter number 2 :";
  std::cin >> y;
  std::cout << "Bit flips required to convert " << x
            << " to " << y << " is " << countBitFlips(x, y) << std::endl;
  return 0;
}

LANGUAGE:

DARK MODE: