-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbitbybit.java
66 lines (50 loc) · 1.52 KB
/
bitbybit.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
64
65
66
import java.util.Scanner;
public class bitbybit {
public static String and(String a, String b){
if(a.equals("0") || a.equals("0"))
return "0";
if(a.equals("1") || b.equals("1"))
return a.equals("1") ? b : a;
return "?";
}
public static String or(String a, String b){
if(a.equals("1") || a.equals("1"))
return "1";
if(a.equals("0") || b.equals("0"))
return a.equals("0") ? b : a;
return "?";
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
loop : while(scan.hasNextLine()){
String[] bits = {"?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?"};
int commands = Integer.valueOf((scan.nextLine()));
if(commands == 0)
break loop;
while(commands --> 0){
String command = scan.next();
if(command.equals("SET")){
bits[scan.nextInt()] = "1";
}
else if(command.equals("CLEAR")){
bits[scan.nextInt()] = "0";
}
else if(command.equals("AND")){
int ind1 = scan.nextInt();
int ind2 = scan.nextInt();
bits[ind1] = and(bits[ind1],bits[ind2]);
}
else{
int ind1 = scan.nextInt();
int ind2 = scan.nextInt();
bits[ind1] = or(bits[ind1],bits[ind2]);
}
}
for(int i=31; i >= 0; i--)
System.out.print(bits[i]);
scan.nextLine();
System.out.println();
}
scan.close();
}
}