-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhalf_subtractor.rs
35 lines (32 loc) · 1.03 KB
/
half_subtractor.rs
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
use crate::model::gates::and_gate::AndGate;
use crate::model::gates::xor_gate::XorGate;
use crate::model::gates::not_gate::NotGate;
use crate::model::subtractor::SubtractorResult;
use crate::model::gates::Gate;
// Declare HalfSubtractor struct
pub struct HalfSubtractor {
and_gate: AndGate,
xor_gate: XorGate,
not_gate: NotGate,
}
// Implement the previously declared HalfSubtractor struct
impl HalfSubtractor {
// Returns a new HalfSubtractor object
pub fn new() -> HalfSubtractor {
return HalfSubtractor {
and_gate: AndGate {},
xor_gate: XorGate {},
not_gate: NotGate {},
};
}
// Calculates the result of parameter a and b
pub fn calc(&self, a: bool, b: bool) -> SubtractorResult {
// Return the result
return SubtractorResult {
// Set diff to result of xor(a, b)
diff: self.xor_gate.calc(a, b),
// Set borrow to and(not(a), b)
borrow: self.and_gate.calc(self.not_gate.calc(a), b),
};
}
}