-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBit.java
More file actions
75 lines (69 loc) · 2.45 KB
/
Bit.java
File metadata and controls
75 lines (69 loc) · 2.45 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
public class Bit {
// Enum values to store whether a bit is true or false.
public enum boolValues { FALSE, TRUE }
// Value of the bit to be stored for later use.
private boolValues ValueStored;
// Constructor
public Bit(boolean value) {
if(value)
ValueStored = boolValues.TRUE;
else
ValueStored = boolValues.FALSE;
}
// Getter/Setter
public boolValues getValue() {
return ValueStored;
}
public void assign(boolValues value) {
ValueStored = value;
}
// and method checks to see if the current bit and the incoming bit is true or false. Can be used with one or two params.
public void and(Bit b2, Bit result) {
and(this, b2, result);
}
public static void and(Bit b1, Bit b2, Bit result) {
if(b1.getValue() == boolValues.TRUE && b2.getValue() == boolValues.TRUE)
result.assign(boolValues.TRUE);
else
result.assign(boolValues.FALSE);
}
// or method used to check if either one or the other bit is true. If so then assign the value to true.
public void or(Bit b2, Bit result) {
or(this, b2, result);
}
public static void or(Bit b1, Bit b2, Bit result) {
if(b1.getValue() == boolValues.TRUE || b2.getValue() == boolValues.TRUE)
result.assign(boolValues.TRUE);
else
result.assign(boolValues.FALSE);
}
// xor checks to see if each bit is equal to each other. Assign value to true if so (if theyre equal), otherwise set it to false.
public void xor(Bit b2, Bit result) {
xor(this, b2, result);
}
public static void xor(Bit b1, Bit b2, Bit result) {
if(b1.getValue() != b2.getValue())
result.assign(boolValues.TRUE);
else
result.assign(boolValues.FALSE);
}
// not modifies the ValueStored variable to either true or false depending on its current value.
public static void not(Bit b2, Bit result) {
if(b2.getValue() == boolValues.TRUE)
result.assign(boolValues.FALSE);
else
result.assign(boolValues.TRUE);
}
public void not(Bit result) {
not(this, result);
}
// toString returns f or t depending on the bit value.
public String toString() {
if(ValueStored == boolValues.TRUE)
return "t";
else if(ValueStored == boolValues.FALSE)
return "f";
else
return "";
}
}