Logic Gates Manually

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite
1












Make a program that simulates the basic logic gates.



Input: An all-caps word followed by 2 1 digit binary numbers, separated by spaces, such as "OR 1 0". The gates OR, AND, NOR, NAND, XOR, and XNOR are needed.



Output: What the output of the entered logic gate would be given the two numbers: either 1 or 0.



Examples:

AND 1 0 becomes 0

XOR 0 1 becomes 1

OR 1 1 becomes 1

NAND 1 1 becomes 0



This is codegolf, so the shortest code wins.










share|improve this question





















  • Can we take in an array as input?
    – Quintec
    2 hours ago











  • no @Quintec you cant
    – qazwsx
    2 hours ago






  • 1




    Can we output as True/False?
    – xnor
    1 hour ago










  • sure @xnor (also relevant username)
    – qazwsx
    1 hour ago














up vote
3
down vote

favorite
1












Make a program that simulates the basic logic gates.



Input: An all-caps word followed by 2 1 digit binary numbers, separated by spaces, such as "OR 1 0". The gates OR, AND, NOR, NAND, XOR, and XNOR are needed.



Output: What the output of the entered logic gate would be given the two numbers: either 1 or 0.



Examples:

AND 1 0 becomes 0

XOR 0 1 becomes 1

OR 1 1 becomes 1

NAND 1 1 becomes 0



This is codegolf, so the shortest code wins.










share|improve this question





















  • Can we take in an array as input?
    – Quintec
    2 hours ago











  • no @Quintec you cant
    – qazwsx
    2 hours ago






  • 1




    Can we output as True/False?
    – xnor
    1 hour ago










  • sure @xnor (also relevant username)
    – qazwsx
    1 hour ago












up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





Make a program that simulates the basic logic gates.



Input: An all-caps word followed by 2 1 digit binary numbers, separated by spaces, such as "OR 1 0". The gates OR, AND, NOR, NAND, XOR, and XNOR are needed.



Output: What the output of the entered logic gate would be given the two numbers: either 1 or 0.



Examples:

AND 1 0 becomes 0

XOR 0 1 becomes 1

OR 1 1 becomes 1

NAND 1 1 becomes 0



This is codegolf, so the shortest code wins.










share|improve this question













Make a program that simulates the basic logic gates.



Input: An all-caps word followed by 2 1 digit binary numbers, separated by spaces, such as "OR 1 0". The gates OR, AND, NOR, NAND, XOR, and XNOR are needed.



Output: What the output of the entered logic gate would be given the two numbers: either 1 or 0.



Examples:

AND 1 0 becomes 0

XOR 0 1 becomes 1

OR 1 1 becomes 1

NAND 1 1 becomes 0



This is codegolf, so the shortest code wins.







code-golf






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 hours ago









qazwsx

5151410




5151410











  • Can we take in an array as input?
    – Quintec
    2 hours ago











  • no @Quintec you cant
    – qazwsx
    2 hours ago






  • 1




    Can we output as True/False?
    – xnor
    1 hour ago










  • sure @xnor (also relevant username)
    – qazwsx
    1 hour ago
















  • Can we take in an array as input?
    – Quintec
    2 hours ago











  • no @Quintec you cant
    – qazwsx
    2 hours ago






  • 1




    Can we output as True/False?
    – xnor
    1 hour ago










  • sure @xnor (also relevant username)
    – qazwsx
    1 hour ago















Can we take in an array as input?
– Quintec
2 hours ago





Can we take in an array as input?
– Quintec
2 hours ago













no @Quintec you cant
– qazwsx
2 hours ago




no @Quintec you cant
– qazwsx
2 hours ago




1




1




Can we output as True/False?
– xnor
1 hour ago




Can we output as True/False?
– xnor
1 hour ago












sure @xnor (also relevant username)
– qazwsx
1 hour ago




sure @xnor (also relevant username)
– qazwsx
1 hour ago










2 Answers
2






active

oldest

votes

















up vote
2
down vote














Python 2, 38 bytes





lambda s:sum(map(ord,s))*3%61%37%9%7%2


Try it online!



A good ol' modulo chain applied to the sum of ASCII values of the input string, making for a solution that's just overfitting. The total ASCII value is distinct for each possible input, except that those with 0 1 and 1 0 give the same result, which is works out because all the logic gates used are symmetric.



The *3 separates out otherwise-adjacent values for inputs that different only in the bits, since these make it hard for the mod chain to split up. The length and size of numbers in the mod chain creates roughly the right amount of entropy to fit 18 binary outputs.



A shorter solution is surely possible using hash(s) or id(s), but I avoided these because they are system-dependent.





Python 2, 50 bytes





lambda s:'_AX0NRD'.find((s*9)[35])>>s.count('0')&1


Try it online!



A slightly more principled solution. Each logic gate gives a different result for each count of zeroes in the input, encodable as a three-bit number from 1 to 6. Each possible logic gate is mapped to the corresponding number by taking (s*9)[35], which are all distinct. For OR, this winds up reading one of the bits so the character could be 0 or 1, but it turns out to work to check if it's 0, and a 1 will correctly give a 1 result anyway.






share|improve this answer




















  • Damn, I was looking for my own mod values, but you beat me to it. Have you written down your strategies for this anywhere, cos my brute force methods tend to take a very long time
    – Jo King
    22 secs ago

















up vote
1
down vote














JavaScript (Node.js), 106 94 bytes





x=>([a,c,d]=x.split` `,g=[c&d,c^d,c|d]["OR".search(a.slice(1+(b=/N[^D]/.test(a))))+1],b?1-g:g)


Try it online!



Link to the code and all 24 cases.



+9 for forgotten to map the XNOR case.






share|improve this answer






















  • maybe I'm not seeing something, but where to I type the input? The input tab does nothing.
    – qazwsx
    2 hours ago










  • @qazwsx This is a lambda function, which by default is allowed.
    – Shieru Asakoto
    2 hours ago







  • 1




    @qazwsx The link shows the outputs for every possible input. This answer is a function, so if you want to manually test it, you can replace the footer with e.g. console.log(f("AND", 1, 1));
    – Mego♦
    2 hours ago










  • @qazwsx And please revert the downvote. This is not invalid after all.
    – Shieru Asakoto
    1 hour ago











  • oh I see. i reverted the downvote
    – qazwsx
    1 hour ago










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "200"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f173492%2flogic-gates-manually%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote














Python 2, 38 bytes





lambda s:sum(map(ord,s))*3%61%37%9%7%2


Try it online!



A good ol' modulo chain applied to the sum of ASCII values of the input string, making for a solution that's just overfitting. The total ASCII value is distinct for each possible input, except that those with 0 1 and 1 0 give the same result, which is works out because all the logic gates used are symmetric.



The *3 separates out otherwise-adjacent values for inputs that different only in the bits, since these make it hard for the mod chain to split up. The length and size of numbers in the mod chain creates roughly the right amount of entropy to fit 18 binary outputs.



A shorter solution is surely possible using hash(s) or id(s), but I avoided these because they are system-dependent.





Python 2, 50 bytes





lambda s:'_AX0NRD'.find((s*9)[35])>>s.count('0')&1


Try it online!



A slightly more principled solution. Each logic gate gives a different result for each count of zeroes in the input, encodable as a three-bit number from 1 to 6. Each possible logic gate is mapped to the corresponding number by taking (s*9)[35], which are all distinct. For OR, this winds up reading one of the bits so the character could be 0 or 1, but it turns out to work to check if it's 0, and a 1 will correctly give a 1 result anyway.






share|improve this answer




















  • Damn, I was looking for my own mod values, but you beat me to it. Have you written down your strategies for this anywhere, cos my brute force methods tend to take a very long time
    – Jo King
    22 secs ago














up vote
2
down vote














Python 2, 38 bytes





lambda s:sum(map(ord,s))*3%61%37%9%7%2


Try it online!



A good ol' modulo chain applied to the sum of ASCII values of the input string, making for a solution that's just overfitting. The total ASCII value is distinct for each possible input, except that those with 0 1 and 1 0 give the same result, which is works out because all the logic gates used are symmetric.



The *3 separates out otherwise-adjacent values for inputs that different only in the bits, since these make it hard for the mod chain to split up. The length and size of numbers in the mod chain creates roughly the right amount of entropy to fit 18 binary outputs.



A shorter solution is surely possible using hash(s) or id(s), but I avoided these because they are system-dependent.





Python 2, 50 bytes





lambda s:'_AX0NRD'.find((s*9)[35])>>s.count('0')&1


Try it online!



A slightly more principled solution. Each logic gate gives a different result for each count of zeroes in the input, encodable as a three-bit number from 1 to 6. Each possible logic gate is mapped to the corresponding number by taking (s*9)[35], which are all distinct. For OR, this winds up reading one of the bits so the character could be 0 or 1, but it turns out to work to check if it's 0, and a 1 will correctly give a 1 result anyway.






share|improve this answer




















  • Damn, I was looking for my own mod values, but you beat me to it. Have you written down your strategies for this anywhere, cos my brute force methods tend to take a very long time
    – Jo King
    22 secs ago












up vote
2
down vote










up vote
2
down vote










Python 2, 38 bytes





lambda s:sum(map(ord,s))*3%61%37%9%7%2


Try it online!



A good ol' modulo chain applied to the sum of ASCII values of the input string, making for a solution that's just overfitting. The total ASCII value is distinct for each possible input, except that those with 0 1 and 1 0 give the same result, which is works out because all the logic gates used are symmetric.



The *3 separates out otherwise-adjacent values for inputs that different only in the bits, since these make it hard for the mod chain to split up. The length and size of numbers in the mod chain creates roughly the right amount of entropy to fit 18 binary outputs.



A shorter solution is surely possible using hash(s) or id(s), but I avoided these because they are system-dependent.





Python 2, 50 bytes





lambda s:'_AX0NRD'.find((s*9)[35])>>s.count('0')&1


Try it online!



A slightly more principled solution. Each logic gate gives a different result for each count of zeroes in the input, encodable as a three-bit number from 1 to 6. Each possible logic gate is mapped to the corresponding number by taking (s*9)[35], which are all distinct. For OR, this winds up reading one of the bits so the character could be 0 or 1, but it turns out to work to check if it's 0, and a 1 will correctly give a 1 result anyway.






share|improve this answer













Python 2, 38 bytes





lambda s:sum(map(ord,s))*3%61%37%9%7%2


Try it online!



A good ol' modulo chain applied to the sum of ASCII values of the input string, making for a solution that's just overfitting. The total ASCII value is distinct for each possible input, except that those with 0 1 and 1 0 give the same result, which is works out because all the logic gates used are symmetric.



The *3 separates out otherwise-adjacent values for inputs that different only in the bits, since these make it hard for the mod chain to split up. The length and size of numbers in the mod chain creates roughly the right amount of entropy to fit 18 binary outputs.



A shorter solution is surely possible using hash(s) or id(s), but I avoided these because they are system-dependent.





Python 2, 50 bytes





lambda s:'_AX0NRD'.find((s*9)[35])>>s.count('0')&1


Try it online!



A slightly more principled solution. Each logic gate gives a different result for each count of zeroes in the input, encodable as a three-bit number from 1 to 6. Each possible logic gate is mapped to the corresponding number by taking (s*9)[35], which are all distinct. For OR, this winds up reading one of the bits so the character could be 0 or 1, but it turns out to work to check if it's 0, and a 1 will correctly give a 1 result anyway.







share|improve this answer












share|improve this answer



share|improve this answer










answered 15 mins ago









xnor

87.3k17181432




87.3k17181432











  • Damn, I was looking for my own mod values, but you beat me to it. Have you written down your strategies for this anywhere, cos my brute force methods tend to take a very long time
    – Jo King
    22 secs ago
















  • Damn, I was looking for my own mod values, but you beat me to it. Have you written down your strategies for this anywhere, cos my brute force methods tend to take a very long time
    – Jo King
    22 secs ago















Damn, I was looking for my own mod values, but you beat me to it. Have you written down your strategies for this anywhere, cos my brute force methods tend to take a very long time
– Jo King
22 secs ago




Damn, I was looking for my own mod values, but you beat me to it. Have you written down your strategies for this anywhere, cos my brute force methods tend to take a very long time
– Jo King
22 secs ago










up vote
1
down vote














JavaScript (Node.js), 106 94 bytes





x=>([a,c,d]=x.split` `,g=[c&d,c^d,c|d]["OR".search(a.slice(1+(b=/N[^D]/.test(a))))+1],b?1-g:g)


Try it online!



Link to the code and all 24 cases.



+9 for forgotten to map the XNOR case.






share|improve this answer






















  • maybe I'm not seeing something, but where to I type the input? The input tab does nothing.
    – qazwsx
    2 hours ago










  • @qazwsx This is a lambda function, which by default is allowed.
    – Shieru Asakoto
    2 hours ago







  • 1




    @qazwsx The link shows the outputs for every possible input. This answer is a function, so if you want to manually test it, you can replace the footer with e.g. console.log(f("AND", 1, 1));
    – Mego♦
    2 hours ago










  • @qazwsx And please revert the downvote. This is not invalid after all.
    – Shieru Asakoto
    1 hour ago











  • oh I see. i reverted the downvote
    – qazwsx
    1 hour ago














up vote
1
down vote














JavaScript (Node.js), 106 94 bytes





x=>([a,c,d]=x.split` `,g=[c&d,c^d,c|d]["OR".search(a.slice(1+(b=/N[^D]/.test(a))))+1],b?1-g:g)


Try it online!



Link to the code and all 24 cases.



+9 for forgotten to map the XNOR case.






share|improve this answer






















  • maybe I'm not seeing something, but where to I type the input? The input tab does nothing.
    – qazwsx
    2 hours ago










  • @qazwsx This is a lambda function, which by default is allowed.
    – Shieru Asakoto
    2 hours ago







  • 1




    @qazwsx The link shows the outputs for every possible input. This answer is a function, so if you want to manually test it, you can replace the footer with e.g. console.log(f("AND", 1, 1));
    – Mego♦
    2 hours ago










  • @qazwsx And please revert the downvote. This is not invalid after all.
    – Shieru Asakoto
    1 hour ago











  • oh I see. i reverted the downvote
    – qazwsx
    1 hour ago












up vote
1
down vote










up vote
1
down vote










JavaScript (Node.js), 106 94 bytes





x=>([a,c,d]=x.split` `,g=[c&d,c^d,c|d]["OR".search(a.slice(1+(b=/N[^D]/.test(a))))+1],b?1-g:g)


Try it online!



Link to the code and all 24 cases.



+9 for forgotten to map the XNOR case.






share|improve this answer















JavaScript (Node.js), 106 94 bytes





x=>([a,c,d]=x.split` `,g=[c&d,c^d,c|d]["OR".search(a.slice(1+(b=/N[^D]/.test(a))))+1],b?1-g:g)


Try it online!



Link to the code and all 24 cases.



+9 for forgotten to map the XNOR case.







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 hours ago

























answered 2 hours ago









Shieru Asakoto

1,790311




1,790311











  • maybe I'm not seeing something, but where to I type the input? The input tab does nothing.
    – qazwsx
    2 hours ago










  • @qazwsx This is a lambda function, which by default is allowed.
    – Shieru Asakoto
    2 hours ago







  • 1




    @qazwsx The link shows the outputs for every possible input. This answer is a function, so if you want to manually test it, you can replace the footer with e.g. console.log(f("AND", 1, 1));
    – Mego♦
    2 hours ago










  • @qazwsx And please revert the downvote. This is not invalid after all.
    – Shieru Asakoto
    1 hour ago











  • oh I see. i reverted the downvote
    – qazwsx
    1 hour ago
















  • maybe I'm not seeing something, but where to I type the input? The input tab does nothing.
    – qazwsx
    2 hours ago










  • @qazwsx This is a lambda function, which by default is allowed.
    – Shieru Asakoto
    2 hours ago







  • 1




    @qazwsx The link shows the outputs for every possible input. This answer is a function, so if you want to manually test it, you can replace the footer with e.g. console.log(f("AND", 1, 1));
    – Mego♦
    2 hours ago










  • @qazwsx And please revert the downvote. This is not invalid after all.
    – Shieru Asakoto
    1 hour ago











  • oh I see. i reverted the downvote
    – qazwsx
    1 hour ago















maybe I'm not seeing something, but where to I type the input? The input tab does nothing.
– qazwsx
2 hours ago




maybe I'm not seeing something, but where to I type the input? The input tab does nothing.
– qazwsx
2 hours ago












@qazwsx This is a lambda function, which by default is allowed.
– Shieru Asakoto
2 hours ago





@qazwsx This is a lambda function, which by default is allowed.
– Shieru Asakoto
2 hours ago





1




1




@qazwsx The link shows the outputs for every possible input. This answer is a function, so if you want to manually test it, you can replace the footer with e.g. console.log(f("AND", 1, 1));
– Mego♦
2 hours ago




@qazwsx The link shows the outputs for every possible input. This answer is a function, so if you want to manually test it, you can replace the footer with e.g. console.log(f("AND", 1, 1));
– Mego♦
2 hours ago












@qazwsx And please revert the downvote. This is not invalid after all.
– Shieru Asakoto
1 hour ago





@qazwsx And please revert the downvote. This is not invalid after all.
– Shieru Asakoto
1 hour ago













oh I see. i reverted the downvote
– qazwsx
1 hour ago




oh I see. i reverted the downvote
– qazwsx
1 hour ago

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f173492%2flogic-gates-manually%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

Long meetings (6-7 hours a day): Being “babysat” by supervisor

What does second last employer means? [closed]

One-line joke