Created
November 13, 2019 13:16
-
-
Save nuclearace/6de5c982f1d7ca0418bab6d8b725026e to your computer and use it in GitHub Desktop.
adder
This file contains hidden or 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
| typealias FourBit = (Int, Int, Int, Int) | |
| func halfAdder(_ a: Int, _ b: Int) -> (Int, Int) { | |
| return (a ^ b, a & b) | |
| } | |
| func fullAdder(_ a: Int, _ b: Int, carry: Int) -> (Int, Int) { | |
| let (s0, c0) = halfAdder(a, b) | |
| let (s1, c1) = halfAdder(s0, carry) | |
| return (s1, c0 | c1) | |
| } | |
| func fourBitAdder(_ a: FourBit, _ b: FourBit) -> (FourBit, carry: Int) { | |
| let (sum1, carry1) = halfAdder(a.3, b.3) | |
| let (sum2, carry2) = fullAdder(a.2, b.2, carry: carry1) | |
| let (sum3, carry3) = fullAdder(a.1, b.1, carry: carry2) | |
| let (sum4, carryOut) = fullAdder(a.0, b.0, carry: carry3) | |
| return ((sum4, sum3, sum2, sum1), carryOut) | |
| } | |
| let a = (0, 1, 1, 0) | |
| let b = (0, 1, 1, 0) | |
| print("\(a) + \(b) = \(fourBitAdder(a, b))") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment