-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongword.java
225 lines (205 loc) · 8.21 KB
/
Longword.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package ComputerArchitecture;
/*
NAME: JAN KARL GALIA
CLASS: COMPUTER ARCHITECTURE
*/
public class Longword {
private final Bit[] bitArray = new Bit[32]; //Creates collection of Bits of size 32
public Longword() {
for (int i = 0; i < 32; i++) {//Initialize all elements of array to false
bitArray[i] = new Bit();
bitArray[i].set(false);
}
}
Bit getBit(int i) {//#1
return bitArray[i]; //Returns the element in index of bitArray
}
public void setBit(int i, Bit value) {//#2
bitArray[i].set(value.getValue()); //Sets value of the index in bitArray
}
public Longword and(Longword otherInput) {//#3 and two Longwords, returning a third
Longword input = new Longword();
for (int i = 0; i < 32; i++) {
input.setBit(i, bitArray[i]); //Copies bitArrays contents into input bitArray
}
Longword output = new Longword();
for (int i = 0; i < 32; i++) {
output.setBit(i, input.getBit(i).and(otherInput.getBit(i)));
}
return output;
}
public Longword or(Longword otherInput) {//#4
Longword input = new Longword();
for (int i = 0; i < 32; i++) {
input.setBit(i, bitArray[i]); //Copies bitArrays contents into input bitArray
}
Longword output = new Longword();
Bit value = new Bit();//Input for setBit
for (int i = 0; i < 32; i++) {
if (input.getBit(i).or(otherInput.getBit(i)).getValue()) {//Bit input and bit otherInput result
value.set(true);//If result is true
output.setBit(i, value); //Places true output bitArray
}
}
return output;
}
public Longword xor(Longword otherInput) {//#5
Longword input = new Longword();
for (int i = 0; i < 32; i++) {
input.setBit(i, bitArray[i]); //Copies bitArrays contents into input bitArray
}
Longword output = new Longword();
Bit value = new Bit();//Input for setBit
for (int i = 0; i < 32; i++) {
if (input.getBit(i).xor(otherInput.getBit(i)).getValue()) {//Bit input and bit otherInput result
value.set(true);//If result is true
output.setBit(i, value); //Places true output bitArray
}
}
return output;
}
public Longword not() {//#6, negate this Longword, creating another
Longword input = new Longword();
for (int i = 0; i < 32; i++) {
input.setBit(i, bitArray[i]); //Copies bitArrays contents into input bitArray
}
Longword output = new Longword();
Bit value = new Bit();//Input for setBit
for (int i = 0; i < 32; i++) {
if (input.getBit(i).not().getValue()) {//Bit input and bit otherInput result
value.set(true);//If result is true
output.setBit(i, value); //Places true output bitArray
}
}
return output;
}
public Longword rightShift(int amount) {//#7
Longword input = new Longword();
for (int i = 0; i < 32; i++) {
input.setBit(i, bitArray[i]); //Copies bitArrays contents into input bitArray
}
Longword output = new Longword();
Bit value = new Bit();//Input for setBit
for (int i = 0; i < (32); i++) {//Cuts off right most bits by amount
if (i < (32 - amount)) {
output.setBit(i, input.getBit(i + amount)); //Sets output Bit[i] to the end of cut off
} else {
value.set(false);
output.setBit(i, value);
}
}
return output;
}
public Longword leftShift(int amount) {//#8
Longword input = new Longword();
for (int i = 0; i < 32; i++) {
input.setBit(i, bitArray[i]); //Copies bitArrays contents into input bitArray
}
Longword output = new Longword();
Bit value = new Bit();//Input for setBit
for (int i = 0; i < 32; i++) {//Cuts off right most bits by amount
if (i < (amount)) {
value.set(false);
output.setBit(i, value);
} else {
output.setBit(i, input.getBit(i - amount)); //Sets output Bit[i] to the end of cut off
}
// System.out.print(output.getBit(i).getValue() + ",");
}
return output;
}
@Override
public String toString() {//#9 Converts to String of t and f, Uses the right Binary orientation
String toString = "";
for (int i = 31; i >= 0; i--) {
if (bitArray[i].getValue())
toString = toString + "t";
else if (!bitArray[i].getValue())
toString = toString + "f";
}
return toString;
}
public String toStringConvert() {//#10 Converts to String of 1 and 0, Uses the right Binary orientation
String toString = "";
for (int i = 31; i >= 0; i--) {
if (bitArray[i].getValue()) {
toString = toString + "1";
} else
toString = toString + "0";
}
return toString;
}
public long getUnsigned() {//#11 returns the value of this longword as a long
long output = 0;
long factor = 1;
if (bitArray[0].getValue()) {//Base Case = true, bitArray[0] because for loop does not include base case
output = 1;
}
for (int i = 1; i < 32; i++) {
factor = factor * 2; //Keeps track of value in each binary
if (bitArray[i].getValue())
output = output + factor;
if (!bitArray[i].getValue())
output = output;
}
return output;
}
public int getSigned() {//#12 returns the value of this longword as an int
Longword input = new Longword();
for (int i = 0; i < 32; i++) {
input.setBit(i, bitArray[i]);
}
Bit bitTrue = new Bit();//To set bit value to true
Bit bitFalse = new Bit();//TO set bit value to false
bitTrue.set(true);
bitFalse.set(false);
//Like Unsigned
int output = 0;
int factor = 1;
//If most significant bit is false, then positive value
if (!bitArray[31].getValue()) {
if (bitArray[0].getValue()) {//Base Case = True, bitArray[0] because for loop does not include base case
output = 1;
}
for (int i = 1; i < 31; i++) { //gets value until last bit
factor = factor * 2; //Keeps track of value in each binary
if (input.bitArray[i].getValue())
output = output + factor;
if (!input.bitArray[i].getValue())
output = output;
}
}
//If most significant bit is true, then negative value
if (bitArray[31].getValue()) {
if (!bitArray[0].getValue()) {//Base Case = false, bitArray[0] because for loop does not include base case
output = 1;
}
for (int i = 1; i < 31; i++) { //gets value until last bit
factor = factor * 2; //Keeps track of value in each binary
if (!input.bitArray[i].getValue())
output = output + factor;
if (input.bitArray[i].getValue())
output = output;
}
output = -(output + 1);//Negative 2s is one less than positive 2s
}
return output;
}
public void copy(Longword otherInput) {// #13 copies the values of the bits from another longword into this one
for (int i = 0; i < 32; i++) {
bitArray[i].set(otherInput.getBit(i).getValue());
}
}
public void set(int value) {//#14 set the value of the bits of this longword (used for tests)
Bit bitTrue = new Bit();
bitTrue.set(true);
int trackInt = 0; //Tracks the value of bit
for (int i = 31; i >= 0; i--) {//Starts from end of Binary array
trackInt = (int) (Math.pow(2, i));
if (trackInt <= value) { //Sets output bitArray[i] to true if trackInt <= Value
value = value - (int) (Math.pow(2, i));
bitArray[i].set(true);
}
}
}
}