-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bit.java
147 lines (126 loc) · 3.77 KB
/
Bit.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package ComputerArchitecture;
/*
Jan Karl Galia
Comp. Architecture
HW 1: The Bit
*/
public class Bit {
private boolean boolVal;
private int value;//Value for Constructor 2
public Bit() {//Default Constructor, added so it doesn't get overriden by other constructors
}
public Bit(int bitInput) {//Constructor 2
value = bitInput;
if (bitInput == 1) {
set(true);
}
if (bitInput == 0) {
set(false);
}
}
public void set(boolean value) {//#1 Sets Value of the bit
boolVal = value;
//System.out.println("Set:"+boolVal); Uncomment to test whether set method works
}
public void toggle() {//#2 Changes the value from true to false or false to true
boolVal = !boolVal;
System.out.println("Toggle:" + boolVal);
}
public void set() {//#3
boolVal = true;
System.out.println("SetToTrue:" + boolVal);
}
public void clear() {//#4
boolVal = false;
System.out.println("Clear:" + boolVal);
}
public boolean getValue() {//#5
return boolVal;
}
public Bit and(Bit otherInput) {//#6
Bit inputOutput = new Bit();//Also, the input and also the output
inputOutput.set(boolVal);
//If any are false, then set output to false
if (!inputOutput.getValue()) {
inputOutput.set(false);
return inputOutput;
}
if (!otherInput.getValue()) {
inputOutput.set(false);
return inputOutput;
}
//If both are true then set output to true
if (inputOutput.getValue()) {
if (otherInput.getValue()) {
inputOutput.set(true);
return inputOutput;
}
}
inputOutput.set(false);//If nothing else then set output to false and return
return inputOutput;
}
public Bit or(Bit otherInput) {//#7-Or-- If any are true then set inputOutput to True
Bit inputOutput = new Bit();
inputOutput.set(boolVal);
if (inputOutput.getValue()) {
inputOutput.set(true);
return inputOutput;
}
if (otherInput.getValue()) {
inputOutput.set(true);
return inputOutput;
}
inputOutput.set(false);
return inputOutput;
}
public Bit xor(Bit otherInput) {//#8-Xor
Bit inputOutput = new Bit();
inputOutput.set(boolVal);
if (inputOutput.getValue()) {
if (!otherInput.getValue()) {// 1 xor 0 = 1
inputOutput.set(true);
return inputOutput;
}
}
if (otherInput.getValue()) { //0 xor 1 = 1
if (!inputOutput.getValue()) {
inputOutput.set(true);
return inputOutput;
}
}
inputOutput.set(false);//others = 0
return inputOutput;
}
public Bit not() {//#9-not
Bit inputOutput = new Bit(value);
inputOutput.set(boolVal);
if (inputOutput.getValue()) {// 1 -> 0
inputOutput.set(false);
boolVal= false;
return inputOutput;
}
boolVal = true;
inputOutput.set(true); // 0 -> 1
return inputOutput;
}
public boolean randomBool() {//Either false or true
int max = 1;
int min = 0;
boolean randomBool = false;
int randomInt = ((int) (Math.random() * (max - min + 1) + min));
//Test Values for otherInput
if (randomInt == 1) {
randomBool = true;
}
if (randomInt == 0) {
randomBool = false;
}
return randomBool;
}
@Override
public String toString() {//#10
if (boolVal)
return "t";
return "f";
}
}