forked from careercup/ctci
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion.java
More file actions
31 lines (27 loc) · 799 Bytes
/
Question.java
File metadata and controls
31 lines (27 loc) · 799 Bytes
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
package Question5_5;
import CtCILibrary.AssortedMethods;
public class Question {
public static int bitSwapRequired(int a, int b) {
int count = 0;
for (int c = a ^ b; c != 0; c = c >> 1) {
count += c & 1;
}
return count;
}
public static int bitSwapRequired2(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) {
int a = 23432;
int b = 512132;
System.out.println(a + ": " + AssortedMethods.toFullBinaryString(a));
System.out.println(b + ": " + AssortedMethods.toFullBinaryString(b));
int nbits = bitSwapRequired(a, b);
int nbits2 = bitSwapRequired2(a, b);
System.out.println("Required number of bits: " + nbits + " " + nbits2);
}
}