-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchefAddition.java
More file actions
45 lines (44 loc) · 1.28 KB
/
chefAddition.java
File metadata and controls
45 lines (44 loc) · 1.28 KB
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
import java.util.*;
import java.math.*;
class main{
// static int add(int a , int b){
// if(a==0) return 0;
// int xor = a^b;
// int and = a&b;
// return 1+add(and<<1,xor);
// }
// static int add(int a , int b){
// int count=0;
// while(b>0){
// int xor = a^b;
// int and = a&b;
// b=and<<1;
// a=xor;
// count++;
// }
// return count;
// }
static int add(BigInteger a , BigInteger b){
int count=0;
while(b.compareTo(BigInteger.ZERO)>0){
BigInteger xor = a.xor(b);
BigInteger and = a.and(b);
b=and.shiftLeft(1);
a=xor;
count++;
}
System.out.println(a.intValue());
return count;
}
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
StringBuilder output = new StringBuilder();
for(int tc=scn.hasNextInt()? scn.nextInt():0;tc>0;tc--){
String s1 = scn.hasNext()?scn.next():"";
String s2 = scn.hasNext()?scn.next():"";
int res = add(new BigInteger(s1,2),new BigInteger(s2,2));
output.append(res+"\n");
}
System.out.println(output);
}
}