-
-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy pathConversion.java
63 lines (56 loc) · 1.85 KB
/
Conversion.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.ctci.bitmanipulation;
/**
* @author rampatra
* @since 2019-03-17
*/
public class Conversion {
/**
* Write a function to determine the number of bits you would need to flip to convert
* integer A to integer B.
* Example:
* Input: 29 (or: 11101), 15 (or: 01111)
* Output: 2
*
* @param a
* @param b
* @return the number of bits to flip
*/
private static int getNoOfBitsToFlipToConvertAToB(int a, int b) {
return countSetBits(a ^ b);
}
private static int countSetBits(int n) {
int count = 0;
while (n > 0) {
if ((n & 1) == 1) {
count++;
}
n >>>= 1;
}
return count;
}
/**
* In this approach, we first take the xor of both the integers (which sets the bits at positions where the bits
* in a and b are different). We then unset the least significant bit in each iteration (c & (c - 1)) and count the
* number of iterations to find the bits to flip.
*
* @param a
* @param b
* @return the number of bits to flip
*/
private static int getNoOfBitsToFlipToConvertAToBWithoutRightShift(int a, int b) {
int count = 0;
for (int c = a ^ b; c != 0; c = c & (c - 1)) {
count++;
}
return count;
}
public static void main(String[] args) {
System.out.println(getNoOfBitsToFlipToConvertAToB(5, 7));
System.out.println(getNoOfBitsToFlipToConvertAToB(5, 5));
System.out.println(getNoOfBitsToFlipToConvertAToB(29, 15));
System.out.println("---");
System.out.println(getNoOfBitsToFlipToConvertAToBWithoutRightShift(5, 7));
System.out.println(getNoOfBitsToFlipToConvertAToBWithoutRightShift(5, 5));
System.out.println(getNoOfBitsToFlipToConvertAToBWithoutRightShift(29, 15));
}
}