Determine the number of bits required to flip if you want to convert integer n to integer m.
Have you met this question in a real interview?
Yes
Example
Given n = 31
(11111), m = 14
(01110), return 2
.
Note
Both n and m are 32-bit integers.
class Solution { /** *@param a, b: Two integer *return: An integer */ public static int bitSwapRequired(int a, int b) { // write your code here if (a == b) { return 0; } int bit = a ^ b; int count = 0; // integer has 32 bits int number = 32; // you cannot just check bit > 0 in the while statement // because a or b maybe negative number while (number > 0) { count += bit & 1; bit = bit >> 1; number--; } return count; } };