-
Notifications
You must be signed in to change notification settings - Fork 19
/
bitbybit.java
68 lines (49 loc) · 1.1 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
67
68
import java.util.Scanner;
public class bitbybit {
public static char OR(char a , char b) {
if (a == '1' || b == '1')
return '1';
if (a == '?' || b == '?')
return '?';
return '0';
}
public static char AND(char a , char b) {
if (a == '0' || b == '0')
return '0';
if (a == '?' || b == '?')
return '?';
return '1';
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (true)
{
int instructions = scan.nextInt();
if (instructions == 0)
break;
char[] bits = "????????????????????????????????".toCharArray();
while (instructions --> 0)
{
String op = scan.next();
if (op.equals("CLEAR"))
bits[scan.nextInt()] = '0';
else if (op.equals("SET"))
bits[scan.nextInt()] = '1';
else if (op.equals("OR"))
{
int i = scan.nextInt();
int j = scan.nextInt();
bits[i] = OR(bits[i] , bits[j]);
}
else if (op.equals("AND"))
{
int i = scan.nextInt();
int j = scan.nextInt();
bits[i] = AND(bits[i] , bits[j]);
}
}
System.out.println(new StringBuilder(new String(bits)).reverse());
}
scan.close();
}
}