-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWord16.java
More file actions
120 lines (116 loc) · 4.17 KB
/
Word16.java
File metadata and controls
120 lines (116 loc) · 4.17 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
public class Word16 {
// Content stores an array of 16 bits.
private Bit[] Content;
// Constructor for bit
public Word16() {
Content = new Bit[16];
for(int i = 0; i < 16; i++)
Content[i] = new Bit(false);
}
// Constructor for Word16 if its to be initialized with an already predefined set of bit values.
public Word16(Bit[] in) {
Content = in;
}
// Copy modifies the input as a copy of the values within the Content var.
public void copy(Word16 result) { // sets the values in "result" to be the same as the values in this instance; use "bit.assign"
for(int i = 0; i < 16; i++)
result.setBitN(i, Content[i]);
}
// setBitN sets the Content at index n of Bit source.
public void setBitN(int n, Bit source) { // sets the nth bit of this word to "source"
Content[n].assign(source.getValue());
}
// returns the index of a certain bit as index n
public void getBitN(int n, Bit result) { // sets result to be the same value as the nth bit of this word
result.assign(Content[n].getValue());
}
// equals checks to see if Word16 input is equal to Content. Can have 1 or 2 inputs.
public boolean equals(Word16 other) { // is other equal to this
for(int i = 0; i < 16; 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(Word16 a, Word16 b) {
return a.equals(b);
}
// equals checks to see if Word16 input is equal to Content. Can have 1 or 2 inputs.
public boolean equals(Word16 other, int num) { // is other equal to this
for(int i = 0; i < num; 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(Word16 a, Word16 b, int num) {
return a.equals(b, num);
}
// and checks to see if each bit from other and result are true. If not then set it to either true or false.
public void and(Word16 other, Word16 result) {
for(int i = 0; i < 16; 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(Word16 a, Word16 b, Word16 result) {
a.and(b, result);
}
// or calls each bit from content[i].or with the bit[i] from word16 b
public void or(Word16 other, Word16 result) {
for(int i = 0; i < 16; 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(Word16 a, Word16 b, Word16 result) {
a.or(b, result);
}
// xor returns true if there's differences between Content and other.
public void xor(Word16 other, Word16 result) {
for(int i = 0; i < 16; 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(Word16 a, Word16 b, Word16 result) {
a.xor(b, result);
}
// not makes 'not' for every index in Content.
public void not( Word16 result) {
for(int i = 0; i < 16; i++){
Bit res = new Bit(false);
Content[i].not(res);
result.setBitN(i, res);
}
}
public static void not(Word16 a, Word16 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();
}
}