Skip to content

Instantly share code, notes, and snippets.

@Ch-sriram
Created May 30, 2020 02:12
Show Gist options
  • Select an option

  • Save Ch-sriram/2ec6bd3a3b9df442ce53ae260304f73e to your computer and use it in GitHub Desktop.

Select an option

Save Ch-sriram/2ec6bd3a3b9df442ce53ae260304f73e to your computer and use it in GitHub Desktop.
Given two integers A & B, find the number of bits that need to be flipped in B to convert it to A.
#include <iostream>
using namespace std;
#define ui uint32_t
ui bit_flips(ui a, ui b) {
ui flips = 0;
for(int i = 0; i < 32; ++i, a>>=1, b>>=1)
if ((a&1) ^ (b&1))
++flips;
return flips;
}
int main() {
int t; cin >> t;
while (t--) {
ui a, b; cin >> a >> b;
cout << bit_flips(a, b) << "\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment