Created
February 25, 2016 11:11
-
-
Save shawnsmithdev/bc9235ca33a50d4e263f to your computer and use it in GitHub Desktop.
nand logic
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
import ( | |
"fmt" | |
) | |
func nand(x, y bool) bool { return !(x && y) } | |
func not(x bool) bool { return nand(x, x) } | |
func and(x, y bool) bool { return not(nand(x, y)) } | |
func or(x, y bool) bool { return nand(not(x), not(y)) } | |
func nor(x, y bool) bool { return not(or(x, y)) } | |
func xor(x, y bool) bool { return nand(nand(x, nand(x, y)), nand(nand(x, y), y)) } | |
func xnor(x, y bool) bool { return not(xor(x, y)) } | |
func reportOp(x, y bool, opName string, op func(bool, bool) bool) { | |
fmt.Printf("%v %v %v is %v\n", x, opName, y, op(x, y)) | |
} | |
func report(x, y bool) { | |
fmt.Printf("=== ( %v, %v ) ===\n", x, y) | |
reportOp(x, y, "nand", nand) | |
fmt.Printf("not %v is %v\n", x, not(x)) | |
reportOp(x, y, "and", and) | |
reportOp(x, y, "or", or) | |
reportOp(x, y, "nor", nor) | |
reportOp(x, y, "xor", xor) | |
reportOp(x, y, "xnor", xnor) | |
fmt.Println() | |
} | |
func main() { | |
report(false, false) | |
report(false, true) | |
report(true, false) | |
report(true, true) | |
} | |
// Output: | |
// | |
// === ( false, false ) === | |
// false nand false is true | |
// not false is true | |
// false and false is false | |
// false or false is false | |
// false nor false is true | |
// false xor false is false | |
// false xnor false is true | |
// === ( false, true ) === | |
// false nand true is true | |
// not false is true | |
// false and true is false | |
// false or true is true | |
// false nor true is false | |
// false xor true is true | |
// false xnor true is false | |
// === ( true, false ) === | |
// true nand false is true | |
// not true is false | |
// true and false is false | |
// true or false is true | |
// true nor false is false | |
// true xor false is true | |
// true xnor false is false | |
// === ( true, true ) === | |
// true nand true is false | |
// not true is false | |
// true and true is true | |
// true or true is true | |
// true nor true is false | |
// true xor true is false | |
// true xnor true is true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment