# Golf all the 16 logic gates with 2 inputs and 1 output!

For example, the gate A and B is a logic gate with 2 inputs and 1 output.

There are exactly 16 of them, because:

• each logic gate takes two inputs, which can be truthy or falsey, giving us 4 possible inputs
• of the 4 possible inputs, each can have an output of truthy and falsey
• therefore, there are 2^4 possible logic gates, which is 16.

Your task is to write 16 programs/functions which implement all of them separately.

They are valid as long as they output truthy/falsey values, meaning that you can implement A or B in Python as lambda a,b:a+b, even if 2 is produced for A=True and B=True.

Score is total bytes used for each function/program.

## List of logic gates

1. 0,0,0,0 (false)
2. 0,0,0,1 (and)
3. 0,0,1,0 (A and not B)
4. 0,0,1,1 (A)
5. 0,1,0,0 (not A and B)
6. 0,1,0,1 (B)
7. 0,1,1,0 (xor)
8. 0,1,1,1 (or)
9. 1,0,0,0 (nor)
10. 1,0,0,1 (xnor)
11. 1,0,1,0 (not B)
12. 1,0,1,1 (B implies A)
13. 1,1,0,0 (not A)
14. 1,1,0,1 (A implies B)
15. 1,1,1,0 (nand)
16. 1,1,1,1 (true)

Where the first number is the output for A=false, B=false, the second number is the output for A=false, B=true, the third number is the output for A=true, B=false, the fourth number is the output for A=true, B=true.

var QUESTION_ID=82938,OVERRIDE_USER=48934;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+(?:\.\d+)?)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

• Your functions/programs may share code. What does this mean? Also, may the programs be in different languages? – Lynn Jun 14 '16 at 23:36
• I find the explanation confusing: "of the 4 possible inputs each can have and output of truthy and falsy". Doesn't this imply 8 (4*2) states? – DavidC Jun 14 '16 at 23:50
• The names you're missing are the AND-NOT gates (A AND NOT B and B AND NOT A). – Mego Jun 15 '16 at 1:33
• So it happened again. There are 18 answer, mostly simple and correct, then out of nowhere the question became "unclear what you're asking". I you don't like a challenge, go on, take another, do not close it! – edc65 Jun 15 '16 at 17:25
• @dorukayhan See: vacuous truth – Sp3000 Jun 15 '16 at 21:19

# Chip, 67 bytes (non-competing, language too recent)

A and B are inputs, a is output.

1. 0000 Empty code produces false

2. 0001 ] is an AND gate

 A
B]a

3. 0010 \ is a switch, where if the top/bottom is false, then left and right are connected

 B
A\a

4. 0011

Aa

5. 0100

 A
B\a

6. 0101

Ba

7. 0110 } is an XOR gate

 A
B}a

8. 0111 juxtaposition implicitly OR's

AaB

9. 1000 ~ is a NOT gates, and ) is an OR gate

 A
B)~a

10. 1001

 A
B}~a

11. 1010

B~a

12. 1011 multiple identical outputs are implicitly OR'd

A~aB

13. 1100

A~a

14. 1101

B~aA

15. 1110 ÷ is a right-to-left NOT gate

A~a÷B

16. 1111 * is always-true

*a


Try it online! Use ASCII 0, 1, 2, and 3 as input, since their low 2 bits are 00, 01, 10, and 11. The extra code of e*f maps the output back to ASCII 0 and 1. You can add a -v flag to see the actual binary in STDERR for the input and output.

• @DLosc Yes, yes I can. Well spotted! – Phlarx Feb 13 '17 at 15:36

# Cubix, 103 bytes

Assuming inputs as 0 for False and 1 for True:

0000    .O@
0001    OI<\a@
0010    OU@a(I<\
0011    .IO@
0100    OU@aI(I/
0101    OIu@
0110    OI<\c@
0111    OI<\b@
1000    OU@(IIb/
1001    OU@+(I<\
1010    OI<\(@
1011    OI<\P@
1100    .I(O@
1101    OU@PsI<\
1110    OU@(aI<\
1111    .1O@


Definitely a lot more that can be golfed here.

# R, 50 bytes

 1. F
2. &
3. >
4. print
5. <
6. "[<-"
7. !=
8. |
9. <!
10. ==
11. !"[<-"
12. ^
13. function(a,b)!a
14. <=
15. !all
16. T


All functions (except 1 and 16 full programs), some infix, a and b taking values in (F,T). 4, 6 and 11 are R specific (TIO link below). I wish I had a good answer also for 13...

Try it online!

• pryr::f(x,y,!x) ties 13 at 15 bytes, but I know virtually no R and can't shorten it further. Maybe you can manage to do so... – Khuldraeseth na'Barya May 30 '18 at 19:53
• @Scrooble Thanks! all my attempts to use pryr::fare failing as I can't get rid of the second argument... – JayCe May 30 '18 at 20:18

# Excel: 84B

0
=a1*b3
=a1>b3
=a1
=a1<b3
=b3
=a1-b3
=a1+b3
=a1+b3=0
=a1=b3
=1-b3
=a1>=b3
=1-a1
=a1<=b3
=a1*b3<1
1

• Not sure if allowed to write FALSE as  to save 1 byte – l4m2 Dec 7 '17 at 5:52
• The use of named sources is not standard for Excel or Google Sheets, you should use the standard input ranges of [A1] and [B1] instead, that said, great submission – Taylor Scott Dec 18 '17 at 14:43
• @Taylor Scott It's like banning to input via arguments and require to use fixed given name in JavaScript, totally going against the rule – l4m2 Dec 18 '17 at 19:11
• ok not that same but both named source and A1 are similar – l4m2 Dec 18 '17 at 19:13
• As was decided by the community, this is invalid. please consider correcting it – Taylor Scott Jul 13 '18 at 16:26

# Flobnar, 68 bytes

Programs containing & require the -d flag.

1 (0)

0@


2 (A&B)

&
*@


3 (A&!B)

&
@


4 (A)

&@


5 (!A&B)

!
*@
&


6 (B)

&
|@&


7 (A^B)

&
-@


8 (A|B)

&
+@


9 (A!|B)

&
+!@


10 (A!^B)

&
-!@


11 (!B)

&
|!@&


12 (A|!B)

&
+@
!


13 (!A)

&!@


14 (!A|B)

&
!@


15 (A!&B)

&
*!@


16 (1)

1@


# Java, 695 bytes

class L{boolean f(){return false;}boolean d(boolean... x){return a(x)==t()?b(x):f();}boolean q(boolean...x){return d(a(x),n(b(x)));}boolean n(boolean x){return x==t()?f():t();}boolean a(boolean... x){return x[0];}boolean w(boolean...x){return d(n(a(x)),b(x));}boolean b(boolean...x){return x[1];}boolean x(boolean...x){return d(o(x),n(d(x)));}boolean o(boolean...x){return n(d(n(a(x)),n(b(x))));}boolean s(boolean...x){return n(o(x));}boolean r(boolean...x){return n(x(x));}boolean l(boolean...x){return n(b(x));}boolean j(boolean...x){return n(w(x));}boolean c(boolean...x){return n(a(x));}boolean v(boolean...x){return n(q(x));}boolean m(boolean...x){return n(d(x));}boolean t(){return !f();}}


Ungolfed

public class LogicGate
{
public static boolean f(boolean... x)
{
return false;
}

public static boolean and(boolean... x)
{
return a(x)==t() ? b(x) : f();
}

public static boolean aandnotb(boolean... x)
{
return and( a(x), not(b(x)) );
}

private static boolean not(boolean x)
{
return x==t()?f():t();
}

public static boolean a(boolean... x)
{
return x[0];
}

public static boolean notaandb(boolean... x)
{
return and( not(a(x)), b(x));
}

public static boolean b(boolean... x)
{
return x[1];
}

public static boolean xor(boolean... x)
{
return and( or(x), not(and(x)) );
}

public static boolean or(boolean... x)
{
return not( and( not(a(x)), not(b(x))) );//see de morgan's law
}

public static boolean nor(boolean... x)
{
return not(or(x));
}

public static boolean xnor(boolean... x)//it is understandable when written nxor. See https://en.wikipedia.org/wiki/XNOR_gate
{
return not(xor(x));
}

public static boolean notb(boolean... x)
{
return not(b(x));
}

public static boolean bimpliesa(boolean... x) //A ⇒ B is true only in the case that either A is false o B is true.
{
return not(notaandb(x)) ;// == o( n(b(x)), a(x));
}

public static boolean nota(boolean... x)
{
return not(a(x));
}

public static boolean aimpliesb(boolean... x)
{
return not(aandnotb(x));
}

public static boolean nand(boolean... x)
{
return not( and(x) );
}

public static boolean t(boolean... x)
{
return !f();
}
}


Test case to try above code.

• You can just write the functions without declaring class. – Leaky Nun Jun 17 '16 at 11:25
• Also, we decided that the programs cannot share code. – Leaky Nun Jun 17 '16 at 11:26
• Also, x==t()? can be replaced with x?, not with !, and you can use the && and || operators. – Leaky Nun Jun 17 '16 at 11:29
• Also, all the boolean in the function type declarations can be changed to Object. – Leaky Nun Jun 17 '16 at 11:30
• @LeakyNun That's a lot of comments. O_O – user48538 Jul 22 '16 at 14:00

# Sesos, 56 55 bytes (non-competing)

; shared by all programs, counted for each
set numin,set numout

; 0,0,0,0, 1 byte
put
; 0,0,0,1, 3 bytes
get,jmp,get,fwd 1,jnz,rwd 1,put
; 0,0,1,0, 4 bytes
get,fwd 1,get,sub 1,jmp,rwd 1,jnz,fwd 2,put
; 0,0,1,1, 1 byte
get,put
; 0,1,0,0, 4 bytes
get,sub 1,fwd 1,get,jmp,rwd 1,jnz,fwd 2,put
; 0,1,0,1, 2 bytes
get,get,put
; 0,1,1,0, 5 bytes
get,fwd 1,get,jmp,rwd 1,sub 1,fwd 1,sub 1,jnz,rwd 1,put
; 0,1,1,1, 5 bytes
get,fwd 1,get,jmp,rwd 1,add 1,fwd 1,sub 1,jnz,rwd 1,put
; 1,0,0,0, 5 bytes
add 1,fwd 1,get,jmp,rwd 1,jnz,get,jmp,rwd 1,jnz,rwd 1,put
; 1,0,0,1, 7 bytes
get,fwd 1,get,jmp,rwd 1,sub 1,fwd 1,sub 1,jnz,add 1,rwd 1,jmp,fwd 1,jnz,fwd 1,put
; 1,0,1,0, 2 bytes
get,get,sub 1,put
; 1,0,1,1, 4 bytes
add 2,fwd 1,get,fwd 1,get,jmp,rwd 1,jnz,fwd 1,sub 1,put
; 1,1,0,0, 2 bytes
get,sub 1,put
; 1,1,0,1, 5 bytes
get,sub 1,fwd 1,get,jmp,rwd 1,sub 1,fwd 1,sub 1,jnz,rwd 1,put
; 1,1,1,0, 4 bytes
get,fwd 1,get,jmp,rwd 1,jnz,fwd 2,sub 1,put
; 1,1,1,1, 1 byte


Input is 0 or 1, output is zero (falsy) or non-zero (truthy).

Thanks to @MartinEnder for golfing off 1 byte!

• Can you include all the hexdumps? – Leaky Nun Jul 28 '16 at 3:45
• Can you mark this answer as non-competing? – Leaky Nun Jul 28 '16 at 3:45
• Ah, forgot it was non-competing. I'll add the hexdumps (and permalinks) after trying to golf it a bit more. – Dennis Jul 28 '16 at 3:52

# Golfscript, 36 bytes

0000 ;
0001 ~*
0010 ~>
0011 ~;
0100 ~<
0101 ~\;
0110 ~^
0111 ~|
1000 ~*!
1001 ~=
1010 ~\;!
1011 ~?
1100 ~;!
1101 ~>!
1110 ~*!
1111


# RProgN, 52 Bytes Non-Competing

0000    [ [ 0   # Pop A and B off reg, push 0 (Falsey)
0001    &       # Push Logical And of A AND B
0010    ! &     # Invert B, Logical AND
0011    [       # Pop B
0100    \ ! &   # Push B under A, Invert A, Logical AND.
0101    \ [     # Push B under A, pop A.
0110    == !    # Pop and And B and push truthy if they're equal. Logical not the output.
0111    |       # Logical Or of A OR B
1000    | !     # Logical Or of A OR B, Inverted.
1001    ==      # Pop and And B and push truthy if they're equal, falsy if they're not.
1010    \ [ !   # Flip B under A, Pop A, Pop B and return it's logical Not.
1011    ! |     # Pop B and push its logical Not, Logical Or of A OR (NOT B)
1100    [ !     # Pop B, pop A then Push the not value of A
1101    \ ! |   # Swap B under A, Pop A and Push it's logical Not, Push the Logical Or of A OR B
1110    & !     # Push Logical And of A AND B, pop and push the logical not.
1111    [ [ 1   # Push A and B off reg, Push 1 (Truthy)


ALL of these are full programs. Four digits behind and text after and including the # are all for commentary.

• If the date on the github is correct you should mark this as non-competing – Wheat Wizard Oct 21 '16 at 5:48

# Java 8, tested in Java 7 and written over directly, 565 bytes

This is mainly as a proof of concept, because I wanted to make a program for this where every gate could be written by only changing a number. It's not nearly as short as some of the other answers, but I felt like posting it here regardless.

interface G{int g(boolean a,boolean b);G[]g={(a,b)->0&(1<<((a?0:2)+(b?0:1))),(a,b)->1&(1<<((a?0:2)+(b?0:1))),(a,b)->2&(1<<((a?0:2)+(b?0:1))),(a,b)->3&(1<<((a?0:2)+(b?0:1))),(a,b)->4&(1<<((a?0:2)+(b?0:1))),(a,b)->5&(1<<((a?0:2)+(b?0:1))),(a,b)->6&(1<<((a?0:2)+(b?0:1))),(a,b)->7&(1<<((a?0:2)+(b?0:1))),(a,b)->8&(1<<((a?0:2)+(b?0:1))),(a,b)->9&(1<<((a?0:2)+(b?0:1))),(a,b)->10&(1<<((a?0:2)+(b?0:1))),(a,b)->11&(1<<((a?0:2)+(b?0:1))),(a,b)->12&(1<<((a?0:2)+(b?0:1))),(a,b)->13&(1<<((a?0:2)+(b?0:1))),(a,b)->14&(1<<((a?0:2)+(b?0:1))),(a,b)->15&(1<<((a?0:2)+(b?0:1)))};}


Behold this disgusting mess. Ungolfed:

interface G{
int g(boolean a, boolean b);
G[] g = {
(a, b) -> 0 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
(a, b) -> 1 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
(a, b) -> 2 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
(a, b) -> 3 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
(a, b) -> 4 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
(a, b) -> 5 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
(a, b) -> 6 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
(a, b) -> 7 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
(a, b) -> 8 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
(a, b) -> 9 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
(a, b) -> 10 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
(a, b) -> 11 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
(a, b) -> 12 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
(a, b) -> 13 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
(a, b) -> 14 & (1 << ((a ? 0 : 2) + (b ? 0 : 1))),
(a, b) -> 15 & (1 << ((a ? 0 : 2) + (b ? 0 : 1)))
// ^ this number + 1 is the logic gate being programmed
};
}


# BitCycle, 140 134 bytes (non-competing)

BitCycle is a language that operates on bits, which makes it good for this challenge. However, it's a 2D language and doesn't have any boolean operators, which makes it less good for this challenge.

### false, 2 bytes

0!


### and, 11 bytes

 !+
?=/!
?^


### A and not B, 11 bytes

 !+
?=/!
?~


### A, 2 bytes

?!


### not A and B, 14 11 bytes

+~
!=
?^
?^


### B, 3 bytes

??!


### xor, 13 10 bytes

?v
?v!
!=~


### or, 12 bytes

?v
/=/!
!
?^


### nor, 13 bytes

?\!+
?\
~=/!


### xnor, 11 bytes

?v
~=\!
!?^


### not B, 6 bytes

 !?
?~


### B implies A, 13 bytes

?v
?=\!
+~


### not A, 5 bytes

 !
?~


### A implies B, 9 bytes

?=\!
?/+~


### nand, 13 bytes

!~
?+
~\\
!?^


### true, 2 bytes

1!


## General explanation

Bits move around the playfield, interacting with various devices. If they exit the playfield, they are destroyed. Here are the devices used in the following programs:

• ! - send bits to output
• ? - get bits from input. Multiple instances of ? are assigned to inputs top-to-bottom, left-to-right. So in these programs, the topmost ? is the A input and the bottommost is B.
• > < ^ v - redirect bits unconditionally
• / and \ - reflect the first bit they encounter, pass all subsequent bits straight through
• = - pass first bit straight through; if first bit was 0, send all subsequent bits west; if first bit was 1 send all subsequent bits east
• + - 0 turns left, 1 turns right
• ~ - bit turns right; a negated copy is generated and turns left

## Ruby, 246 bytes, non-competing

Not really golfed, and somewhat boring. But I can't leave this question without a Ruby answer.

1    ->a,b{0[a+a+b]}
2    ->a,b{1[a+a+b]}
3    ->a,b{2[a+a+b]}
4    ->a,b{3[a+a+b]}
5    ->a,b{4[a+a+b]}
6    ->a,b{5[a+a+b]}
7    ->a,b{6[a+a+b]}
8    ->a,b{7[a+a+b]}
9    ->a,b{8[a+a+b]}
10   ->a,b{9[a+a+b]}
11   ->a,b{10[a+a+b]}
12   ->a,b{11[a+a+b]}
13   ->a,b{12[a+a+b]}
14   ->a,b{13[a+a+b]}
15   ->a,b{14[a+a+b]}
16   ->a,b{15[a+a+b]}


A lot of improvements are possible, for example rewriting the first function as ->a,b{} but even assuming the function body is 1 byte for each answer, the minimum byte count is 64.

Based on the python non-competing lambda, this could be written as:

->n{->a,b{n[a+a+b]}}


# Pyth, 33 bytes

0000: 0
0001: &E
0010: <E
0011: Q
0100: >E
0101: E
0110: xE
0111: |E
1000: !|E
1001: qE
1010: !E
1011: !>E
1100: !
1101: !<E
1110: !&E
1111: 1


Mind the trailing spaces! Test here, putting each program in the Code box.

• I'm surprised there isn't an answer in Pyth. – Leaky Nun Jun 18 '17 at 14:08
• I think you can drop out those Es on those two-byte solutions, since they are actually functions. – Leaky Nun Jun 18 '17 at 14:09
• Also a bare E wouldn't work. – Leaky Nun Jun 18 '17 at 14:09
• Nor would !E. – Leaky Nun Jun 18 '17 at 14:10
• @LeakyNun Not sure what would a function be like in Pyth. – Erik the Outgolfer Jun 18 '17 at 14:16

# Wise, 46 bytes (not-competing)

## False

^:^


## And

&


## A and not B

?~&


## A

?:^|


## not A and B

~&


## B

:^|


## Xor

^


## Or

|


## Nor

|~


## Xnor

^~


## Not B

:^|~


## B implies A

~&~


## Not A

?:^^~


## A implies B

?~&~


## Nand

~?~&


## True

^:~^


D,0000,,0
D,0001,@@,&
D,0010,@@,!$& D,0011,@, D,0100,@@,$!&
D,0101,@@,
D,0110,@@,Bx
D,0111,@@,Bo
D,1000,@@,Bo!
D,1001,@@,Bx!
D,1010,@@,!
D,1011,@@,!Bo
D,1100,@,!
D,1101,@@,!&!
D,1110,@@,&!
D,1111,,1


The byte count assumes single byte names, such as a or b

## How they work

### 0000

D,0000,,     - Create a niladic function
0  - Return 0


### 0001

D,0001,@@,   - Create a dyadic function
&  - Return A and B


### 0010

D,0010,@@,   - Create a dyadic function
!  - NOT B
$- Swap & - Return A and NOT B  ### 0011 D,0011,@, - Create a monadic function - Return A  ### 0100 D,0100,@@, - Create a dyadic function$  - Swap
!  - NOT A
&  - Return NOT A and B


### 0101

D,0101,@@,   - Create a dyadic function
- Return B


### 0110

D,0110,@@,   - Create a dyadic function
Bx - Return A xor B


### 0111

D,0111,@@,   - Create a dyadic function
Bo - Return A or B


### 1000

D,1000,@@,   - Create a dyadic function
Bo - A or B
!  - Return NOT A or NOT B


### 1001

D,1001,@@,   - Create a dyadic function
Bx - A xor B
!  - Return NOT A xor NOT B


### 1010

D,1010,@@,   - Create a dyadic function
!  - Return NOT B


### 1011

D,1011,@@,   - Create a dyadic function
!  - NOT B
Bo - Return A or NOT B


### 1100

D,1100,@,    - Create a monadic function
!   - Return NOT A


### 1101

D,1101,@@,   - Create a dyadic function
!  - NOT B
&  - A and NOT B
!  - Return NOT A and B


### 1110

D,1110,@@,   - Create a dyadic function
&  - A and B
!  - Return NOT A and NOT B


### 1111

D,1111,,     - Create a niladic function
1    - Return 1

• "each logic gate takes two inputs" - can the niladic functions take arguments? Because if not, this seems invalid – FlipTack Nov 18 '17 at 17:56
• @FlipTack A niladic function can be given as many arguments as it wants, it'll just ignore them. For instance, try adding $f>1>2>3\nO to the bottom of the last one on TIO (\n is a literal new line). The output doesn't change. – caird coinheringaahing Nov 18 '17 at 17:59 • Hm that's what I presumed, just wanted to check – FlipTack Nov 18 '17 at 18:17 # Implicit, 35 28 bytes 0000 false # push length of stack 0001 p and q * 2 implicit int inputs, multiply 0010 p and not q < 2 implicit int inputs, less than 0011 p$      read input
0100 not p and q        >      2 implicit int inputs, greater than
0101 q                  $$read input twice 0110 xor · 2 implicit int inputs, XOR 0111 p or q + 2 implicit int inputs, add 1000 not p and not q ññ= implicit input NOT-equals self, input not-equals self, equality 1001 eq = 2 implicit int inputs, equality 1010 not q$$ñ    read two integers, second NOT-equals self
1011 p or not q         ñ+   read two integers, second NOT-equals self, add
1100 not p              ñ      implicit input, NOT-equals self
1101 not p or q         ñ\$+    implicit input NOT-equals self, read input, add
1110 not p or not q     *ñ     2 implicit int inputs, multiply, logical-NOT
1111 true               1      push 1


All of these rely on implicit output.

# Pyth - 75 bytes

False

0


And

AQ&GH


A and not B

AQ&G!H


A

AQG


Not A and B

AQ&!GH


B

AQH


XOR

AQxGH


OR

AQ+GH


NOR

AQ!+GH


XNOR

AQ!xGH


Not B

AQ!H


B implies A

AQ!&!GH


not A

AQ!G


A implies b

AQ!&G!H


nand

AQ!&GH


True

1


# Pyt, 46 bytes

0000   0
0001   ←←∧
0010   ←←¬∧
0011   ←
0100   ←¬←∧
0101   ←ŕ←
0110   ←←⊻
0111   ←←∨
1000   ←←⊽
1001   ←←⊙
1010   ←ŕ←¬
1011   ←¬←⊼
1100   ←¬
1101   ←←¬⊼
1110   ←←⊼
1111   1


# Excel VBA, 114 Bytes

Series of binary Boolean operators which give the individually specified commented to the right of the function. All take input, if any, as A from [A1] and B from [B1] on the ActiveSheet object

Interestingly, despite the presence of OR, AND, NOT, XOR and IMP operators, using any of these directly is actually longer than using math.

?0          ' 0,0,0,0 (false)
?[A1*B1]    ' 0,0,0,1 (and)
?[A1>B1]    ' 0,0,1,0 (A and not B)
?[A1]       ' 0,0,1,1 (A)
?[A1<B1]    ' 0,1,0,0 (not A and B)
?[B1]       ' 0,1,0,1 (B)
?[A1-B1]    ' 0,1,1,0 (xor)
?[A1+B1]    ' 0,1,1,1 (or)
?[A1+B1=0]  ' 1,0,0,0 (nor)
?[A1=B1]    ' 1,0,0,1 (xnor)
?[1-B1]     ' 1,0,1,0 (not B)
?[A1>=B1]   ' 1,0,1,1 (B implies A)
?[1-A1]     ' 1,1,0,0 (not A)
?[A1<=B1]   ' 1,1,0,1 (A implies B)
?1>[A1*B1]  ' 1,1,1,0 (nand)
?1          ' 1,1,1,1 (true)


## Uncommented Version

For Byte Counting

?0
?[A1*B1]
?[A1>B1]
?[A1]
?[A1<B1]
?[B1]
?[A1-B1]
?[A1+B1]
?[A1+B1=0]
?[A1=B1]
?[1-B1]
?[A1>=B1]
?[1-A1]
?[A1<=B1]
?1>[A1*B1]
?1


# C (gcc), 144 143 bytes

#define Q(R,S)R(a,b){return S>>a+a+b&1;}
Q(A,0)Q(B,8)Q(C,4)Q(D,12)Q(E,2)Q(F,10)Q(G,6)Q(H,14)Q(I,1)Q(J,9)Q(K,5)Q(L,13)Q(M,3)Q(N,11)Q(O,7)Q(P,15)


Try it online!

A(a,b){return 0>>a+a+b&1;}  // false
B(a,b){return 8>>a+a+b&1;}  // and
C(a,b){return 4>>a+a+b&1;}  // A and not B
D(a,b){return 12>>a+a+b&1;} // A
E(a,b){return 2>>a+a+b&1;}  // not A
F(a,b){return 10>>a+a+b&1;} // B
G(a,b){return 6>>a+a+b&1;}  // xor
H(a,b){return 14>>a+a+b&1;} // or
I(a,b){return 1>>a+a+b&1;}  // nor
J(a,b){return 9>>a+a+b&1;}  // xnor
K(a,b){return 5>>a+a+b&1;}  // not B
L(a,b){return 13>>a+a+b&1;} // B implies A
M(a,b){return 3>>a+a+b&1;}  // not A
N(a,b){return 11>>a+a+b&1;} // A implies B
O(a,b){return 7>>a+a+b&1;}  // nand
P(a,b){return 15>>a+a+b&1;} // true

• Thanks @ceilingcat – PrincePolka Jul 27 at 11:26
• I think this answer is invalid because the functions aren't independent (they share the same #define directive). – Erik the Outgolfer Jul 27 at 19:59
• @EriktheOutgolfer what about the other C answer? those arrays share the same typedef – PrincePolka Jul 28 at 17:00
• I'm not sure which answer you're referring to, but I'm pretty sure that sharing the same directive makes all functions dependent on one of them (try isolating each function). The challenge requires that the functions be independent. – Erik the Outgolfer Jul 28 at 18:47

# Jelly, 17 bytes

Gate Name        L Code
---- ----------- - ----
0000 FALSE       1 0
0001 AND         1 &
0010 A AND NOT B 1 >
0011 A           0
0100 NOT A AND B 1 <
0101 B           1 ⁹
0110 XOR         1 ^
0111 OR          1 |
1000 NOR         2 |C
1001 XNOR        1 ⁼
1010 NOT B       1 %
1011 B IMPLIES A 1 :
1100 NOT A       1 C
1101 A IMPLIES B 1 ọ
1110 NAND        2 &C
1111 TRUE        1 1


Test suite.

# Ceylon, 143 bytes

value u=[for(i in 0:16)(Boolean a,Boolean b)=>[false,a&&b,a&&!b,a,!a&&b,b,a!=b,a||b,!a&&!b,a==b,!b,a||!b,!a,!a||b,!a||!b,true][i]else nothing];


Normally you would write it this way:

alias B => Boolean;

B(B, B)[] t = [
(B a, B b) => false,
(B a, B b) => a&&b,
(B a, B b) => a&&!b,
(B a, B b) => a,
(B a, B b) => b&&!a,
(B a, B b) => b,
(B a, B b) => a!=b,
(B a, B b) => a||b,
(B a, B b) => !a&&!b,
(B a, B b) => a==b,
(B a, B b) => !b,
(B a, B b) => a||!b,
(B a, B b) => !a,
(B a, B b) => b||!a,
(B a, B b) => !a||!b,
(B a, B b) => true
];


This alias + variable definition of length 284 (if removing unneeded whitespace) defines t as a sequence of 16 functions, each with two Boolean parameters and a Boolean return value.

xnor is just equality, xor is inequality for booleans. nand and nor were transformed via DeMorgan's rules, as this is shorter than !(a&&b) or !(a||b). A implies B is written in the "not A or B" form.

We added the alias to not have to write Boolean each time – this is worth from three usages. Normally you would have written [B(B, B)*] (or [B(B, B)+]) for the type of t, but the "traditional" syntax with the [] at the end is one byte shorter. (We could also have written B(B, B)[16] to indicate it's a sequence of length 16, but that would have been even longer, and wouldn't work for the next versions anyways.)

Unfortunately, we here have to repeat the parameter types and names each time.

If we are just interested in all the expressions (and don't necessary need functions), then this works too:

alias B => Boolean;

B[] z(B a,B b) => [
false,
a&&b,
a&&!b,
a,
!a&&b,
b,
a!=b,
a||b,
!a&&!b,
a==b,
!b,
a||!b,
!a,
!a||b,
!a||!b,
true
];


This (golfed length 113) is a function z which returns a sequence of 16 Boolean values (the results of each of the gates in order). (This is not much more than the 78 bytes for just the expressions and commas.) We can convert it into the same type as the first one (sequence of functions) by adding this one-liner:

B(B,B)[] w = [for (i in 0:16) (B a, B b) => z(a,b)[i] else nothing];


(The else nothing is needed because the Ceylon compiler is unable to figure out that i is always in the range of valid indexes for z. Alternatively, we could change the type to B?(B,B) (i.e. returning an optional boolean), but then the caller would have to worry about nulls.)

z and w together come to 172 bytes. Of course, we can merge those two together to save a tiny bit more:

alias B=>Boolean;
B(B,B)[] y = [for (i in 0:16)
(B a, B b) => [
false,
a&&b,
a&&!b,
a,
!a&&b,
b,
a!=b,
a||b,
!a&&!b,
a==b,
!b,
a||!b,
!a,
!a||b,
!a||!b,
true
][i] else nothing];


This is 150 bytes (after removing whitespace), less than double of the plain expressions (+ commas) of 76.

If we can use a private declaration (inside a function or class) instead of a shared one, we don't need to write down the type (use the value keyword instead, for type inference), and then can get rid of the alias (as we only have two Boolean left):

value u = [for (i in 0:16)
(Boolean a, Boolean b) => [
false,
a&&b,
a&&!b,
a,
!a&&b,
b,
a!=b,
a||b,
!a&&!b,
a==b,
!b,
a||!b,
!a,
!a||b,
!a||!b,
true
][i] else nothing];


This (with whitespace removal) is the 143 bytes declaration from the top of the post.

Try it online!