-
Notifications
You must be signed in to change notification settings - Fork 0
/
bit.java
180 lines (153 loc) · 4.34 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
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
interface bitOps
{
//Interface: Given by Professor Phipps
void set(boolean value);
void toggle();
void set();
void clear();
boolean getValue();
bit and(bit other);
bit or(bit other);
bit xor(bit other);
bit not();
@Override
String toString();
}
public class bit implements bitOps
{
//Attributes of a bit - value (representing a 1 (true, on) or a 0 (false, off)
private boolean value;
//Default Constructor
public bit()
{
//By default, a bit object will be set to false, (0 or off).
this.value = false;
}
//Parameterized Constructor
//newVal: create a bit object with a desired value (true or false, 1 or 0, on or off)
public bit(boolean newVal)
{
this.value = newVal;
}
//Toggle(): if true, make false. if false, make true.
public void toggle()
{
//If value of said bit is true, set value to false.
if(this.value)
{
this.value = false;
}
//Otherwise, the value is false, so change value to true.
else
{
this.value = true;
}
}
//set(): Force value of bit value to true
public void set()
{
this.value = true;
}
//set(boolean value): Set the bit value to desired boolean value
public void set(boolean value)
{
this.value = value;
}
//clear(): Force the value of the bit to false.
public void clear()
{
this.value = false;
}
//getValue(): return the value of the bit object being called on
public boolean getValue()
{
return this.value;
}
//and(): compare the bit being called and the bit being passed in
//if both bits are true, return a bit with value true, otherwise, return a bit with value false.
public bit and(bit other)
{
bit output = new bit();
//By default, a bit object's value will be false
//If both conditions are not met, will return the default false bit value.
if(this.value == true)
{
if(other.value == true)
{
output.value = true;
}
}
return output;
}
//or(): compare the bit being called and the bit being passed in
//if either one of the bits are true, return a bit with value true.
//if both bits are false, return a bit with value false.
public bit or(bit other)
{
bit output = new bit();
//By default, a bit object's value will be false
//If either one of the conditions are not met,
//will return the default false bit value.
if(this.value == true)
{
output.value = true;
}
else if(other.value == true)
{
output.value = true;
}
return output;
}
//xor(): compare the bit being called and the bit being passed in
//if both bit's values are opposite, return a bit with value true
//otherwise, return a bit with value false
public bit xor(bit other)
{
bit output = new bit();
//If both bits have opposite values, change output to true
if(this.value == true)
{
if(other.value == false)
{
output.value = true;
}
}
//Checking the other way around
if(this.value == false)
{
if(other.value == true)
{
output.value = true;
}
}
//If both true or both false, return false.
return output;
}
//not(): take the value of the bit being called on and negate it
//return a new bit with said value
public bit not()
{
bit output = new bit();
if(this.value)
{
output.value = false;
}
else
{
output.value = true;
}
return output;
}
//Override toString() for bit object
//Should just say "f" if bit's value false or "t" if bit's value true.
@Override
public String toString()
{
String output = "f";
if(this.value == true)
{
output = "t";
}
return output;
}
}