In other words, can I create every standard logic gate without using logical or logical bitwise operators and without using any other operators or constants, except in the first function?
Yes, I can.
bool Nand(bool a, bool b){ return 1-(a*b); } bool Not(bool a){ return Nand(a, a); } bool And(bool a, bool b){ return Not(Nand(a, b)); } bool Nor(bool a, bool b){ return And(Not(a), Not(b)); } bool Or(bool a, bool b){ return Not(Nor(a, b)); } bool Xor(bool a, bool b){ return And(Or(a, b), Not(And(a, b))); } bool Buffer(bool a){ return Not(Not(a)); } bool Xnor(bool a, bool b){ return Not(Xor(a, b)); } bool False(bool a){ return And(a, Not(a)); } bool True(bool a){ return Or(a, Not(a)); }
bool Or(bool a, bool b) {return a+b;}bool And(bool a, bool b) {return a*b;}bool Xor(bool a, bool b) {return a!=b;}bool Nor(bool a, bool b) {return !a*!b;}bool Nand(bool a, bool b) {return 1-a*b;}- bool Nand(bool a, bool b){
- return 1-(a*b);
- }
- bool Not(bool a){
- return Nand(a, a);
- }
- bool And(bool a, bool b){
- return Not(Nand(a, b));
- }
- bool Nor(bool a, bool b){
- return And(Not(a), Not(b));
- }
- bool Or(bool a, bool b){
- return Not(Nor(a, b));
- }
- bool Xor(bool a, bool b){
- return And(Or(a, b), Not(And(a, b)));
- }
- bool Buffer(bool a){
- return Not(Not(a));
- }
- bool Xnor(bool a, bool b){
- return Not(Xor(a, b));
- }
- bool False(bool a){
- return And(a, Not(a));
- }
- bool True(bool a){
- return Or(a, Not(a));
- }
Describe(LOGIC_WITHOUT_LOGIC) { It(AND) { Assert::That(And(true, false), Equals(false)); Assert::That(And(false, true), Equals(false)); Assert::That(And(true, true), Equals(true)); Assert::That(And(false, false), Equals(false)); } It(OR) { Assert::That(Or(true, false), Equals(true)); Assert::That(Or(false, true), Equals(true)); Assert::That(Or(true, true), Equals(true)); Assert::That(Or(false, false), Equals(false)); } It(XOR) { Assert::That(Xor(true, false), Equals(true)); Assert::That(Xor(false, true), Equals(true)); Assert::That(Xor(true, true), Equals(false)); Assert::That(Xor(false, false), Equals(false)); } It(XNOR) { Assert::That(Xnor(true, false), Equals(false)); Assert::That(Xnor(false, true), Equals(false)); Assert::That(Xnor(true, true), Equals(true)); Assert::That(Xnor(false, false), Equals(true)); } It(NOR) { Assert::That(Nor(true, false), Equals(false)); Assert::That(Nor(false, true), Equals(false)); Assert::That(Nor(true, true), Equals(false)); Assert::That(Nor(false, false), Equals(true)); } It(NAND) { Assert::That(Nand(true, false), Equals(true)); Assert::That(Nand(false, true), Equals(true)); Assert::That(Nand(true, true), Equals(false)); Assert::That(Nand(false, false), Equals(true)); } It(NOT) { Assert::That(Not(true), Equals(false)); Assert::That(Not(false), Equals(true)); } It(BUFFER) { Assert::That(Buffer(true), Equals(true)); Assert::That(Buffer(false), Equals(false)); } It(FALSE) { Assert::That(False(false), Equals(false)); Assert::That(False(true), Equals(false)); } It(TRUE) { Assert::That(True(false), Equals(true)); Assert::That(True(true), Equals(true)); } };
Describe(AND_OR_XOR)- Describe(LOGIC_WITHOUT_LOGIC)
- {
- It(AND)
- {
- Assert::That(And(true, false), Equals(false));
- Assert::That(And(false, true), Equals(false));
- Assert::That(And(true, true), Equals(true));
- Assert::That(And(false, false), Equals(false));
- }
- It(OR)
- {
- Assert::That(Or(true, false), Equals(true));
- Assert::That(Or(false, true), Equals(true));
- Assert::That(Or(true, true), Equals(true));
- Assert::That(Or(false, false), Equals(false));
- }
- It(XOR)
- {
- Assert::That(Xor(true, false), Equals(true));
- Assert::That(Xor(false, true), Equals(true));
- Assert::That(Xor(true, true), Equals(false));
- Assert::That(Xor(false, false), Equals(false));
- }
- It(XNOR)
- {
- Assert::That(Xnor(true, false), Equals(false));
- Assert::That(Xnor(false, true), Equals(false));
- Assert::That(Xnor(true, true), Equals(true));
- Assert::That(Xnor(false, false), Equals(true));
- }
- It(NOR)
- {
- Assert::That(Nor(true, false), Equals(false));
- Assert::That(Nor(false, true), Equals(false));
- Assert::That(Nor(true, true), Equals(false));
- Assert::That(Nor(false, false), Equals(true));
- }
- It(NAND)
- {
- Assert::That(Nand(true, false), Equals(true));
- Assert::That(Nand(false, true), Equals(true));
- Assert::That(Nand(true, true), Equals(false));
- Assert::That(Nand(false, false), Equals(true));
- }
- It(NOT)
- {
- Assert::That(Not(true), Equals(false));
- Assert::That(Not(false), Equals(true));
- }
- It(BUFFER)
- {
- Assert::That(Buffer(true), Equals(true));
- Assert::That(Buffer(false), Equals(false));
- }
- It(FALSE)
- {
- Assert::That(False(false), Equals(false));
- Assert::That(False(true), Equals(false));
- }
- It(TRUE)
- {
- Assert::That(True(false), Equals(true));
- Assert::That(True(true), Equals(true));
- }
- };