-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWord32.java
More file actions
125 lines (120 loc) · 4.01 KB
/
Word32.java
File metadata and controls
125 lines (120 loc) · 4.01 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
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
public class Word32 {
// Class to store bit data.
private Bit[] Content;
// Constructor
public Word32() {
Content = new Bit[32];
for(int i = 0; i < 32; i++)
Content[i] = new Bit(false);
}
// Constructor to set a specific value.
public Word32(Bit[] in) {
Content = in;
}
// Sets indexes 0-15 to result.
public void getTopHalf(Word16 result) { // sets result = bits 0-15 of this word. use bit.assign
for(int i = 0; i < 16; i++)
result.setBitN(i, Content[i]);
}
// Sets indexes 16-31 to result.
public void getBottomHalf(Word16 result) { // sets result = bits 16-31 of this word. use bit.assign
for(int i = 16; i < 32; i++)
result.setBitN(i-16, Content[i]);
}
// Copies value to result.
public void copy(Word32 result) { // sets result's bit to be the same as this. use bit.assign
for(int i = 0; i < 32; i++)
result.setBitN(i, Content[i]);
}
// Equals checks to see if the current word32 is equal to the Word32 other.
public boolean equals(Word32 other) { // is other equal to this
for(int i = 0; i < 32; i++){
Bit b2 = new Bit(false);
other.getBitN(i, b2);
Bit res = new Bit(false);
Content[i].xor(b2, res);
if(res.getValue() == Bit.boolValues.TRUE)
return false;
}
return true;
}
public static boolean equals(Word32 a, Word32 b) {
return a.equals(b);
}
// Gets a bit from a certain index n from Content.
public void getBitN(int n, Bit result) {
result.assign(Content[n].getValue());
}
// Sets a bit from a certain index from source.
public void setBitN(int n, Bit source) { // use bit.assign
Content[n].assign(source.getValue());
}
// And checks to see the result for Content and other to get result.
public void and(Word32 other, Word32 result) {
for(int i = 0; i < 32; i++){
Bit b2 = new Bit(false);
other.getBitN(i, b2);
Bit res = new Bit(false);
Content[i].and(b2, res);
result.setBitN(i, res);
}
}
public static void and(Word32 a, Word32 b, Word32 result) {
a.and(b, result);
}
// or calls each bit from content[i].or with the bit[i] from word32 b
public void or(Word32 other, Word32 result) {
for(int i = 0; i < 32; i++){
Bit b2 = new Bit(false);
other.getBitN(i, b2);
Bit res = new Bit(false);
Content[i].or(b2, res);
result.setBitN(i, res);
}
}
public static void or(Word32 a, Word32 b, Word32 result) {
a.or(b, result);
}
// xor returns true if there's differences between Content and other.
public void xor(Word32 other, Word32 result) {
for(int i = 0; i < 32; i++){
Bit b2 = new Bit(false);
other.getBitN(i, b2);
Bit res = new Bit(false);
Content[i].xor(b2, res);
result.setBitN(i, res);
}
}
public static void xor(Word32 a, Word32 b, Word32 result) {
a.xor(b, result);
}
// not makes 'not' for every index in Content.
public void not( Word32 result) {
for(int i = 0; i < 32; i++){
Bit res = new Bit(false);
Content[i].not(res);
result.setBitN(i, res);
}
}
public static void not(Word32 a, Word32 result) {
a.not(result);
}
// toString returns all bits comma separated.
public String toString() {
StringBuilder sb = new StringBuilder();
for (Bit bit : Content) {
sb.append(bit.toString());
sb.append(",");
}
return sb.toString();
}
public boolean checkLast3(Word32 a){
for(int i = 29; i < 32; i++){
Bit b = new Bit(false);
a.getBitN(i, b);
if(Content[i].getValue() != b.getValue())
return false;
}
return true;
}
}