80
\$\begingroup\$

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.

Your functions/programs must be independent.

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.

Leaderboard

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>

\$\endgroup\$
26
  • 2
    \$\begingroup\$ Your functions/programs may share code. What does this mean? Also, may the programs be in different languages? \$\endgroup\$
    – Lynn
    Jun 14, 2016 at 23:36
  • 2
    \$\begingroup\$ 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? \$\endgroup\$
    – DavidC
    Jun 14, 2016 at 23:50
  • 4
    \$\begingroup\$ The names you're missing are the AND-NOT gates (A AND NOT B and B AND NOT A). \$\endgroup\$
    – user45941
    Jun 15, 2016 at 1:33
  • 20
    \$\begingroup\$ 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! \$\endgroup\$
    – edc65
    Jun 15, 2016 at 17:25
  • 5
    \$\begingroup\$ @dorukayhan See: vacuous truth \$\endgroup\$
    – Sp3000
    Jun 15, 2016 at 21:19

94 Answers 94

1
\$\begingroup\$

Pyramid*, 112 bytes

The programs, in order (each separated by a =):

0000 (false):

0<

0001 (and):

?<
?

0010 (A and not B):

0?<
?
1

0011 (A):

?<

0100 (B and not A):

?
?<
0

0101 (B):

?
?<
?

0110 (xor):

?
0
1?<
?
0

0111 (or):

?
?<

1000 (nor):

1
?
00
?<
0

1001 (xnor):

1
?
0?<
1
?

1010 (not B):

1
?
00
?<
11
?
0

1011 (B implies A):

1
?
0?<

1100 (not A):

1
?<
0

1101 (A implies B):

1
?<
?

1110 (nand):

1
?<
11
?
0

1111 (true):

1<

*Technically, all of this can be done using regular Stackylogic.

\$\endgroup\$
1
\$\begingroup\$

Logicode, 282 bytes

All logic gates in order:

out 0
circ g(a,b)->a&b
circ g(a,b)->a&(!b)
circ g(a)->a
circ g(a,b)->(!a)&b
circ g(a,b)->b
circ g(a,b)->(!(a&b))&(a|b)
circ g(a,b)->a|b
circ g(a,b)->!(a|b)
circ g(a,b)->!((!(a&b))&(a|b))
circ g(a,b)->!b
circ g(a,b)->(!a)|b
circ g(a)->!a
circ g(a,b)->a|(!b)
circ g(a,b)->!(a,b)
out 1

Logicode is basically a code version of Logisim.

Check the Github repo (in the link) for more information.

\$\endgroup\$
1
\$\begingroup\$

NOR gates, 42 gates

Exprimed in pseudocode, NOR gates are -

0000 0
0001 (a-a)-(b-b)
0010 (a-a)-b
0011 a
0100 a-(b-b)
0101 b
0110 (a-b-a)-(a-a-b)-(a-b)
0111 (a-b)-(a-b)
1000 a-b
1001 (a-b-a)-(a-a-b)
1010 b-b
1011 (a-b-a)-(a-b-a)
1100 a-a
1101 (b-a-b)-(b-a-b)
1110 (a-a)-(b-b)-((a-a)-(b-b))
1111 1
\$\endgroup\$
5
  • \$\begingroup\$ Unless there is an interpreter, this is invalid. \$\endgroup\$ Oct 16, 2016 at 17:47
  • \$\begingroup\$ @ConorO'Brien Ruby, 37 bytes: class Fixnum;def-(a);self|a^1;end;end \$\endgroup\$ Oct 16, 2016 at 17:48
  • \$\begingroup\$ So then these are only snippets, and do not take input. \$\endgroup\$ Oct 16, 2016 at 17:49
  • 1
    \$\begingroup\$ @ConorO'Brien There is already a solution using NAND gates, and here I just exprimate the gates in pseudocode instead of with a diagram \$\endgroup\$ Oct 16, 2016 at 17:51
  • 2
    \$\begingroup\$ Out of the kindness of my heart, I have made a 172-byte NOR interpreter: class Fixnum;def-(a);self|a^1;end;end;a={"a"=>!!1,"b"=>!!1};c=ARGV.shift;i=ARGV.map &:to_i;k=-1;eval"p "+c.gsub(/a|b/){|m|if a[m];a[m]=!a[m];"(#{m}=i[#{k+=1}])";else;m;end}. Takes input as ruby xor.rb "program" "input a" "input b" \$\endgroup\$ Oct 16, 2016 at 18:08
1
\$\begingroup\$

SmileBASIC, 221 bytes

0     'false
A*B   'and
A>B   'a and not b
A     'a
A<B   'b and not a
B     'b
A-B   'xor
A+B   'or
A+B<1 'nor
A==B  'xnor
!B    'not b
A>=B  'b implies a
!A    'not a
B>=A  'a implies b
A+B<2 'nand
1     'true 

Add INPUT A,B? before each expression.

Other short expressions that didn't make the list (due to being longer, or the same length with not as nice output):

A/B  'divide by 0 error
A!=B 'a xor b, replaced by A-B
A*!B 'a and not b
A>>B 'a and not b, replaced by A>B
A+!B 'b implies a, replaced by A>=B
A-!B 'a xnor b, replaced by A==B
A||B 'a or b, replaced by A+B
A&&B 'a and b, replaced by A*B
\$\endgroup\$
1
  • \$\begingroup\$ The question specifies functions or full programs, not expressions, so unfortunately I think you do need the INPUT A,B?. \$\endgroup\$
    – DLosc
    Feb 11, 2017 at 8:13
1
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ @DLosc Yes, yes I can. Well spotted! \$\endgroup\$
    – Phlarx
    Feb 13, 2017 at 15:36
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
2
  • \$\begingroup\$ 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... \$\endgroup\$ May 30, 2018 at 19:53
  • \$\begingroup\$ @Scrooble Thanks! all my attempts to use pryr::fare failing as I can't get rid of the second argument... \$\endgroup\$
    – JayCe
    May 30, 2018 at 20:18
1
\$\begingroup\$

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
\$\endgroup\$
6
  • \$\begingroup\$ Not sure if allowed to write FALSE as `` to save 1 byte \$\endgroup\$
    – l4m2
    Dec 7, 2017 at 5:52
  • \$\begingroup\$ 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 \$\endgroup\$ Dec 18, 2017 at 14:43
  • \$\begingroup\$ @Taylor Scott It's like banning to input via arguments and require to use fixed given name in JavaScript, totally going against the rule \$\endgroup\$
    – l4m2
    Dec 18, 2017 at 19:11
  • \$\begingroup\$ ok not that same but both named source and A1 are similar \$\endgroup\$
    – l4m2
    Dec 18, 2017 at 19:13
  • \$\begingroup\$ As was decided by the community, this is invalid. please consider correcting it \$\endgroup\$ Jul 13, 2018 at 16:26
1
\$\begingroup\$

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@
\$\endgroup\$
1
\$\begingroup\$

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!

Ungolfed / expanded with comments

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
\$\endgroup\$
4
  • \$\begingroup\$ Thanks @ceilingcat \$\endgroup\$ Jul 27, 2019 at 11:26
  • 2
    \$\begingroup\$ I think this answer is invalid because the functions aren't independent (they share the same #define directive). \$\endgroup\$ Jul 27, 2019 at 19:59
  • \$\begingroup\$ @EriktheOutgolfer what about the other C answer? those arrays share the same typedef \$\endgroup\$ Jul 28, 2019 at 17:00
  • \$\begingroup\$ 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. \$\endgroup\$ Jul 28, 2019 at 18:47
1
\$\begingroup\$

Charcoal, 31 bytes

Charcoal's default input is two 0 or 1 digits separated by a space or newline and default output is - for 1 and empty for 0. Requiring 0 or 1 output would add 12 bytes; the first program would become 0 and most other programs would need an initial . The exceptions would be N (becomes θ), Iη (becomes η) and ¹ (becomes 1).

0 0 0 0         0 bytes  Empty output.
0 0 0 1   ∧NN   3 bytes  Logical And.
0 0 1 0   ›     1 byte   Greater than.
0 0 1 1   N     1 byte   First input.
0 1 0 0   ‹     1 byte   Less than.
0 1 0 1   Iη    2 bytes  Second input as a boolean.
0 1 1 0   ¬⁼    2 bytes  Not equal.
0 1 1 1   ∨NN   3 bytes  Logical Or.
1 0 0 0   ¬∨NN  4 bytes  Logical Nor.
1 0 0 1   ⁼     1 byte   Equals.
1 0 1 0   ¬Iη   3 bytes  Logical Not of second input as a boolean.
1 0 1 1   X     1 byte   Exponentiate.
1 1 0 0   ¬N    2 bytes  Logical Not of first input.
1 1 0 1   ¬›    2 bytes  Not greater than.
1 1 1 0   ¬∧NN  4 bytes  Logical Nand.
1 1 1 1   ¹     1 byte  Prints `-`.
\$\endgroup\$
1
\$\begingroup\$

Julia 1.0, 44 bytes

⊊
&
>
x$_=x
<
foldr
!=
|
!|
==
!foldr
^
x$_=!x
<=
!&
!⊊

Try it online!

based on Dennis's Julia 0.4 answer

\$\endgroup\$
1
\$\begingroup\$

Vyxal, 26 Bytes

0       # 0000: 1 byte
∧      # 0001: 1 byte
¬∧     # 0010: 2 bytes
¹      # 0011: 1 byte
∧¬     # 0010: 2 bytes
²      # 0101: 1 byte
^      # 0110: 1 byte
∨      # 0111: 1 byte
+±     # 1000: 2 bytes
=      # 1001: 1 byte
²¬     # 1010: 2 bytes
∧²¬   # 1011: 3 bytes
¬      # 1100: 1 byte
¬∨    # 1101: 2 bytes
+<2   # 1110: 3 bytes
1     # 1111: 1 byte

Port of Emigna's 05ab1e answer.

\$\endgroup\$
1
\$\begingroup\$

Go, 443 bytes

[]func(B,B)B{func(a,b B)B{return 1<0},func(a,b B)B{return a&&b},func(a,b B)B{return a&&!b},func(a,b B)B{return a},func(a,b B)B{return !a&&b},func(a,b B)B{return b},func(a,b B)B{return a!=b},func(a,b B)B{return a||b},func(a,b B)B{return !(a||b)},func(a,b B)B{return a==b},func(a,b B)B{return !b},func(a,b B)B{return a==b||a},func(a,b B)B{return !a},func(a,b B)B{return a==b||b},func(a,b B)B{return !(a&&b)},func(a,b B)B{return 0<1}}
type B=bool

Attempt This Online!

Really long solution, mostly taken up by boilerplate func(a,b B)B{return ...}. The functions are in the order in the OP.

\$\endgroup\$
1
\$\begingroup\$

tinylisp, 154 bytes

Each solution is a function that takes two arguments which are either 0 or 1 and returns a truthy or falsey value. Falsey values include 0 and (); truthy values include 1, 2, and -1.

Try it online!

false, 1 byte

h

A builtin function that expects only one argument. If given two arguments, it sends an error message to stderr and returns ().

and, 15 bytes

(q((A B)(i A B(

If A is truthy, return B; else, return ().

A and not B, 14 bytes

(q((A B)(l B A

The less-than builtin l with swapped argument order.

A, 8 bytes

(q(_(h _

Take an arbitrary number of arguments; return the first one.

not A and B, 1 byte

l

The less-than builtin.

B, 9 bytes

(q((A B)B

Return B.

xor, 1 byte

s

The subtraction builtin.

or, 1 byte

a

The addition builtin.

nor, 18 bytes

(q((A B)(e 0(a A B

Add A and B and test whether the result equals 0.

xnor, 1 byte

e

The equals builtin.

not B, 15 bytes

(q((A B)(i B()1

If B is truthy, return (); else, return 1.

B implies A, 16 bytes

(q((A B)(i B A 1

If B is truthy, return A; else, return 1.

not A, 14 bytes

(q(_(i(h _)()1

Take an arbitrary number of arguments; if the first one is truthy, return (), else return 1.

A implies B, 16 bytes

(q((A B)(i A B 1

If A is truthy, return B; else, return 1.

nand, 18 bytes

(q((A B)(l(a A B)2

Add A and B and test whether the result is less than 2.

true, 6 bytes

(q(_ 1

Take an arbitrary number of arguments and return 1.

\$\endgroup\$
0
\$\begingroup\$

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.

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

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
add 1,put

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

Thanks to @MartinEnder for golfing off 1 byte!

\$\endgroup\$
1
  • \$\begingroup\$ Can you include all the hexdumps? \$\endgroup\$
    – Leaky Nun
    Jul 28, 2016 at 3:45
0
\$\begingroup\$

Golfscript, 36 bytes

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

Online interpreter.

\$\endgroup\$
0
\$\begingroup\$

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
    };
}
\$\endgroup\$
0
\$\begingroup\$

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.

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

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

^:~^
\$\endgroup\$
0
\$\begingroup\$

Add++, 135 bytes

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
\$\endgroup\$
3
  • \$\begingroup\$ "each logic gate takes two inputs" - can the niladic functions take arguments? Because if not, this seems invalid \$\endgroup\$
    – FlipTack
    Nov 18, 2017 at 17:56
  • \$\begingroup\$ @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. \$\endgroup\$ Nov 18, 2017 at 17:59
  • \$\begingroup\$ Hm that's what I presumed, just wanted to check \$\endgroup\$
    – FlipTack
    Nov 18, 2017 at 18:17
0
\$\begingroup\$

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.

Links: 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

Pyt, 46 bytes

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

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
\$\endgroup\$
0
0
\$\begingroup\$

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!

\$\endgroup\$
0
\$\begingroup\$

Ral, 99 bytes

Note that only the positive integers are truthy in Ral. Zero and negative integers are falsy.

.
1,,+-.
,,/-.
,.
,,-.
,,/.
,,-:1:+:+:+:+?0-.
,,+.
,,+1-.
,,-:1:+:+:+:+?0-1-.
,,1-.
,,-1-.
,1-.
,,-1+.
,,11+--.
1.

Try it online! (all programs and inputs)

\$\endgroup\$
0
\$\begingroup\$

RProgN, 52 Bytes

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.

\$\endgroup\$
0
0
\$\begingroup\$

BQN, 22 bytes

0˙
∧
>
⊣
<
⊢
≠
∨
¬∨
=
¬⊢
≥
¬⊣
≤
¬∧
1˙

Each entry is a dyadic function that returns either 0 or 1. (Other integers cannot be used as truth values in BQN.) Verify all test cases.

Explanation

A ← 0˙ # 0000 Constant function, returns 0
B ← ∧  # 0001 And builtin
C ← >  # 0010 Left arg is greater than right
D ← ⊣  # 0011 Return left arg
E ← <  # 0100 Left arg is less than right
F ← ⊢  # 0101 Return right arg
G ← ≠  # 0110 Args are not equal
H ← ∨  # 0111 Or builtin
I ← ¬∨ # 1000 Not Or
J ← =  # 1001 Args are equal
K ← ¬⊢ # 1010 Not Right
L ← ≥  # 1011 Left arg is greater than or equal to right
M ← ¬⊣ # 1100 Not Left
N ← ≤  # 1101 Left arg is less than or equal to right
O ← ¬∧ # 1110 Not And
P ← 1˙ # 1111 Constant function, returns 1
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.