Created
May 30, 2020 02:12
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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