-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimizexor.java
43 lines (41 loc) · 1.17 KB
/
minimizexor.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
class minimizexor{
public static void main(String[] args){
int num1 = 31,num2 = 3;
// int x = Integer.bitCount(num1);
// System.out.println("no of bit" + x);
int n = 1000;
// int n = (int)Math.pow(10,9);
int count = 0;
int a =0 ;
int count_1;
int min = Integer.MAX_VALUE;
int ans = 0;
while(num2 > 0){
count += num2 & 1;
num2 = num2 >> 1;
}
// System.out.println(count);
int i;
int k;
for(i = 2; i <= n ; i++){
k = i;
count_1 = 0;
while(k > 0){
count_1 += k & 1;
k = k >> 1;
}
if(count_1 == count){
// System.out.println(count + " " + count_1);
a = i ^ num1;
// System.out.println("value of a: " + a);
if(a <= min){
min = a;
// System.out.println("min is :" + min);
ans = i;
// System.out.println("ans is :" + ans);
}
}
}
System.out.println(ans);
}
}