Created
September 14, 2021 00:09
-
-
Save sarmadgulzar/82e0b220ed95e27c53a734ab1b673631 to your computer and use it in GitHub Desktop.
Logic Gates in Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Debug, Clone, Copy)] | |
enum Bit { | |
ZERO, | |
ONE, | |
} | |
impl Bit { | |
fn nand(self, other: Self) -> Self { | |
match self { | |
Self::ONE => match other { | |
Self::ONE => Self::ZERO, | |
_ => Self::ONE, | |
}, | |
_ => Self::ONE, | |
} | |
} | |
fn not(self) -> Self { | |
self.nand(self) | |
} | |
fn and(self, other: Self) -> Self { | |
self.nand(other).not() | |
} | |
fn or(self, other: Self) -> Self { | |
self.not().nand(other.not()) | |
} | |
fn nor(self, other: Self) -> Self { | |
self.or(other).not() | |
} | |
fn xor(self, other: Self) -> Self { | |
self.and(other.not()).or(self.not().and(other)) | |
} | |
fn xnor(self, other: Self) -> Self { | |
self.xor(other).not() | |
} | |
} | |
impl PartialEq for Bit { | |
fn eq(&self, other: &Self) -> bool { | |
match self { | |
Self::ONE => match other { | |
Self::ONE => true, | |
_ => false, | |
}, | |
Self::ZERO => match other { | |
Self::ZERO => true, | |
_ => false, | |
}, | |
} | |
} | |
} | |
fn main() { | |
let mut a: Bit; | |
let mut b: Bit; | |
// NOT | |
a = Bit::ZERO; | |
assert_eq!(Bit::ONE, a.not()); | |
b = Bit::ONE; | |
assert_eq!(Bit::ZERO, b.not()); | |
// AND | |
a = Bit::ZERO; | |
b = Bit::ZERO; | |
assert_eq!(Bit::ZERO, a.and(b)); | |
a = Bit::ZERO; | |
b = Bit::ONE; | |
assert_eq!(Bit::ZERO, a.and(b)); | |
a = Bit::ONE; | |
b = Bit::ZERO; | |
assert_eq!(Bit::ZERO, a.and(b)); | |
a = Bit::ONE; | |
b = Bit::ONE; | |
assert_eq!(Bit::ONE, a.and(b)); | |
// OR | |
a = Bit::ZERO; | |
b = Bit::ZERO; | |
assert_eq!(Bit::ZERO, a.or(b)); | |
a = Bit::ZERO; | |
b = Bit::ONE; | |
assert_eq!(Bit::ONE, a.or(b)); | |
a = Bit::ONE; | |
b = Bit::ZERO; | |
assert_eq!(Bit::ONE, a.or(b)); | |
a = Bit::ONE; | |
b = Bit::ONE; | |
assert_eq!(Bit::ONE, a.or(b)); | |
// NOR | |
a = Bit::ZERO; | |
b = Bit::ZERO; | |
assert_eq!(Bit::ONE, a.nor(b)); | |
a = Bit::ZERO; | |
b = Bit::ONE; | |
assert_eq!(Bit::ZERO, a.nor(b)); | |
a = Bit::ONE; | |
b = Bit::ZERO; | |
assert_eq!(Bit::ZERO, a.nor(b)); | |
a = Bit::ONE; | |
b = Bit::ONE; | |
assert_eq!(Bit::ZERO, a.nor(b)); | |
// XOR | |
a = Bit::ZERO; | |
b = Bit::ZERO; | |
assert_eq!(Bit::ZERO, a.xor(b)); | |
a = Bit::ZERO; | |
b = Bit::ONE; | |
assert_eq!(Bit::ONE, a.xor(b)); | |
a = Bit::ONE; | |
b = Bit::ZERO; | |
assert_eq!(Bit::ONE, a.xor(b)); | |
a = Bit::ONE; | |
b = Bit::ONE; | |
assert_eq!(Bit::ZERO, a.xor(b)); | |
// XNOR | |
a = Bit::ZERO; | |
b = Bit::ZERO; | |
assert_eq!(Bit::ONE, a.xnor(b)); | |
a = Bit::ZERO; | |
b = Bit::ONE; | |
assert_eq!(Bit::ZERO, a.xnor(b)); | |
a = Bit::ONE; | |
b = Bit::ZERO; | |
assert_eq!(Bit::ZERO, a.xnor(b)); | |
a = Bit::ONE; | |
b = Bit::ONE; | |
assert_eq!(Bit::ONE, a.xnor(b)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment