| Bytes | Lang | Time | Link |
|---|---|---|---|
| 216 | BrainFlak | 250117T224454Z | MegaTom |
| 021 | Japt | 250114T123336Z | Shaggy |
| 023 | 05AB1E | 250113T090412Z | Kevin Cr |
| 040 | Bash | 250105T154545Z | GammaFun |
| 059 | AWK | 250103T182127Z | wobtax |
| 048 | Google Sheets | 241227T033519Z | doubleun |
| 048 | Google Sheets | 241228T014451Z | noodle p |
| 025 | Uiua | 241225T160805Z | noodle p |
| 037 | Zsh | 241226T195451Z | GammaFun |
| 051 | Common Lisp | 241226T164654Z | AlephSqu |
| 049 | Perl 5 pl | 241226T060121Z | Xcali |
| 4845 | C | 241225T173721Z | m90 |
| 149 | R | 241225T204937Z | Eonema |
| 018 | Charcoal | 241225T194719Z | Neil |
| 132 | brainfuck | 241225T185339Z | Level Ri |
| 070 | Vim | 241225T190456Z | Fmbalbue |
| 022 | Jelly | 241225T185856Z | Unrelate |
| 045 | JavaScript ES6 | 241225T183731Z | Arnauld |
| 033 | Jelly | 241225T174007Z | Fmbalbue |
| 043 | JavaScript Node.js | 241225T003739Z | l4m2 |
| 056 | APL+WIN | 241225T083443Z | Graham |
| 039 | Retina 0.8.2 | 241225T005726Z | Neil |
| 044 | Python | 241225T000548Z | Unrelate |
Brain-Flak, 216 bytes
((({})<({}[{}]<><(((((((()()()()){}){}()){}){})(()()()()()){})[((()()()){}){}()])>){(<{}(([{}]{}{}())[()()()])>)}{}><>)){{}<>((((()()()){}){}){({}[()])}{})(<>)}{}({}<{}>{}[()]){<>(((((()()()()()){}()){}){}){})(<>)}<>
Japt, 22 21 bytes
Takes input as 4 individual bits, outputs in lowercase.
çÍixsUaX¹+`d `¸gUaV
Try it
Explanation
çÍixsUaX¹+`...`¸gUaV :Implicit input of bits U, V, W (unused) & X
ç :Repeat U times
Í : "n"
i :Prepend
x : "x"
s : slice from 0-based index
UaX : Absolute difference of U & X
¹ :End prepend
+ :Append
`...` : Compressed string "and or"
¸ : Split on spaces
g : Get element at 0-based index
UaV : Absolute difference of U & V
05AB1E, 23 bytes
ćÅ¿'x×Iн'nׄ€—€ƒ#I2£ËèJ
Input as a string.
Try it online or verify all test cases.
Explanation:
ć # Extract head of the (implicit) input-string; push first item and
# remainder-string separately
Å¿ # Check if the remainder-string ends with this first bit
'x× '# Repeat "x" that check (0 or 1) amount of times
I # Push the input-string again
н # Pop and push its first bit
'n× '# Repeat "n" that bit amount of times
„€—€ƒ # Push dictionary string "or and"
# # Split it on spaces: ["or","and"]
I # Push the input-string yet again
2£ # Pop and keep its first two bits
Ë # Check whether they are equal
è # Use that to (0-based) index into the pair
J # Join all three strings on the stack together
# (after which the result is output implicitly)
See this 05AB1E tip of mine (section How to use the dictionary?) to understand why „€—€ƒ is "or and".
Bash, 40 bytes
a=({X,,N,XN}OR {N,}AND)
echo ${a[1$1%6]}
Port of my Zsh port of l4m2's answer. Prefixes the bitstring with a 1 to ensure it isn't interpreted as octal, which means the array entries/equivalence classes shift.
AWK, 61 59 bytes
$1==$4{$3="X"}$1{$4="N"}$5=$1-$2?"OR":"AND",gsub("[01 ]",z)
The input comes with spaces, e.g.
0 0 0 1
1 1 1 0
0 1 1 1
1 0 0 0
0 1 1 0
1 0 0 1
produces:
AND
NAND
OR
NOR
XOR
XNOR
Explanation:
- Check if
$1and$4are the same. In this case$3is no longer needed, so we storeXthere. - We need an
Nexactly when$1is1, and we can use$4to store theNbecause that space is no longer needed. - Set a new column
$5toORif$1and$2differ, orANDif they're the same. - Get rid of bits and spaces, and what remains is the name of the gate.
Google Sheets, 48 bytes
=mid("NANDAND XOR OR NOR XNOR",1+4*mod(A1,6),4)
Put the bits as a text string in cell A1 and the formula in cell B1.

Google Sheets, 50 48 bytes
=if(A1=D1,"X",)&if(A1,"N",)&if(A1=B1,"AND","OR")
Same score as doubleunary's lookup table. Thanks to doubleunary for a suggestion saving 2 bytes. This is my first time trying to code golf in google sheets, so I may have missed something else.
The 4 inputs should be in A1, B1, C1, D1, formula in E1.
Uiua, 25 bytes
⊂⊙⨬"OR""AND"▽⊓⊟"XN"⊃⤚𝄐𝄐==
Try it: Uiua pad. This is inspired by Neil's solution in Charcoal.
Uiua, 31 bytes
⊡◿6⋕⊙{⟜↘₁"NAND"⊂@X.⍥₂⊸↘₁"XNOR"}
Try it: Uiua pad. This is a port of l4m2's smart JavaScript solution, using some nice tricks to write the array shorter in Uiua.
The array needs to be in the order {"NAND" "AND" "XOR" "OR" "NOR" "XNOR"}, so:
⟜↘₁"NAND"pushes "NAND" then its tail "AND"⊂@X.⍥₂⊸↘₁"XNOR"pushes "XNOR", its tail "NOR", its tail "OR", and that with X prepended "XOR", in opposite order to what I just wrote.
Zsh, 37 bytes
Port of l4m2's answer, with brace expansions to shorten it.
Would require tweaks for Bash, since bash interprets 0* strings as octal by default.
a=({N,}AND {X,,N,XN}OR)
<<<$a[1+$1%6]
Common Lisp, 51 bytes
(princ(elt'(NAND AND XOR OR NOR XNOR)(mod(read)6)))
Port of l4m2's Javascript answer. It takes input from STDIN and outputs to STDOUT, since that seemed to be shorter than writing a lambda. Also, symbols are used instead of strings.
C, 48 45 bytes
char*f(x){return"XOR\0NAND\0XNOR"+(18-x)%12;}
Takes an integer.
Makes use of the properties of C's null-terminated strings: they can overlap if one is a suffix of another, and because they are represented by pointers, adding to the pointer shifts the starting point.
R, 149 bytes
`-`=\(p)\(...)!p(...)
\(x)names(which(unlist(lapply(c(AND=`&`,OR=`|`,XOR=xor,NAND=-`&`,NOR=-`|`,XNOR=-xor),\(o)all(o(rep(0:1,2),rep(0:1,e=2))==x)))))
Checks a named list of first-class functions for one that gives the matching results.
Charcoal, 18 bytes
×X⁼θε×NIθ≡θηAND¦OR
Try it online! Link is to verbose version of code. Takes input as four space or newline separated bits. Explanation:
×X⁼θε
If the first and last bits are the same then output an X.
×NIθ
If the first bit is a 1 then output an N. (1 byte could be saved by requiring input in JSON format, which would make the Cast unnecessary.)
≡θηAND¦OR
If the first two bits are the same then output AND otherwise output OR.
brainfuck, 132 bytes
-[--->+<]>[>,]<<<<[->->->-<<<]>>>[>]<<<<[+++.--->]<------->>>>[>]<-[<<<[>]<.>>[>]]<[[<]<+.+++.<]<[>>]<<[>>----[----<+>]<++.<.>+++.>]
Expects input as a string of 4 ascii digits (terminated with zero byte.) The code starts by generating 255/3=85 (ASCII U), then takes the four inputs, and subtracts the first from the rest. This gives the following memory status. It prints an X if the last cell is zero, an N if the last nonzero is negative, followed by OR if the cell next to it is nonzero (or AND if it is zero.)
0001 -> AND 0 85 0 0 0 1
1110 -> NAND 0 85 0 0 0 -1
0111 -> OR 0 85 0 1 1 1
1000 -> NOR 0 85 0 -1 -1 -1
0110 -> XOR 0 85 0 1 1 0
1001 -> XNOR 0 85 0 -1 -1 0
Developed at https://minond.xyz/brainfuck/ . Brief comments below will be improved later.
-[--->+<] Countdown cell 0 to setup 255/3=85 in cell 1
>[>,]<<<< Take input in cells 2~5 & move to cell 2
[->->->-<<<]>>> Decrement all inputs until cell 2 = 0
[>]<<<<[+++.--->] If cell 5 = 0 print X and end in cell 2
<------->>>>[>]< Prepare cell 1 to print N and move to last nonzero cell
-[<<<[>]<.>>[>]] Decrement last nonzero cell and If still nonzero it must be negative so print N
<[[<]<+.+++.<] If cell to the left is nonzero print OR and leave pointer to left of all data
<[>>]<< Search to the left of the current cell for the cell containing N
[>> If found generate the ASCII for A
----[----<+>]<++.(256-4)/4+2=65 and print it
<.>+++.>] Followed by ND
Vim, 70 bytes
llx:s/001/AND
:s/010/XOR
:s/011/OR
:s/100/NOR
:s/101/XNOR
:s/110/NAND
Port of this answer made by Unrelated String.
Explanation: remove the third character, then make the appropriate substitutions. Probably the simplest answer.
Jelly, 22 bytes
“u{nµỤẈiBÞ]Ṛ[)®Æ8Ẇ»Ḳị@
Direct port of l4m2's decimal lookup table. Jelly doesn't have any such conversion from string to decimal that can handle leading zeroes as desired, so if decimal input isn't acceptable, add +1 byte for a Ḍ at the end taking list input. A permutation index or other magic formula might be able to make exploiting the structure of the output more competitive, but I've got a lunch to eat, and I'm mostly just disappointed that this beats:
Jelly, 24 bytes
^\Ḅ^1‘ị⁾XNŒPp@“AND“OR”¤Ṛ
Takes input as a flat list of truth values.
^\ Scan by XOR. Since the middle two bits are always equal,
^\Ḅ ‘ị bit 1 is copied to bit 3, and thus affects 2 and 4 equally.
Ḅ Convert to binary.
^1 XOR with 1 (i.e. flip bit 4).
‘ị ¤ 0-index (mod 8) into:
“AND“OR” ["AND", "OR"]'s
p@ ordered Cartesian product with
⁾XNŒP the powerset of "XN".
Ṛ Reverse the obtained pair (which is implicitly smash-printed).
JavaScript (ES6), 45 bytes
A solution without a lookup table.
Expects a 4-character binary string.
x=>"XN".slice(x%9%2,1-x&9)+(x%6<2?"AND":"OR")
JavaScript (Node.js), 43 bytes
x=>'NAND1AND1XOR1OR1NOR1XNOR'.split(1)[x%6]
-3 bytes tsh
Trivial. (if) Porting Unrelated String would make it longer
APL+WIN, 56 bytes
Prompts for a vector of ones and zeros:
('AND' 'NAND' 'OR' 'NOR' 'XOR' 'XNOR')['147869'⍳⍕10|2⊥⎕]
Retina 0.8.2, 41 39 bytes
(.)..\1
X$&
1...
N$&
(.)\1..
AND
\d+
OR
Try it online! Link includes test cases. Explanation:
(.)..\1
X$&
Prefix X if the first and last digits are the same.
1...
N$&
Prefix N to the first digit if it is a 1.
(.)\1..
AND
Replace the digits with AND if the first and second are the same.
\d+
OR
Otherwise replace the digits with OR.
Python, 46 44 bytes
lambda a,b,_,d:'XN'[a^d:-~a]+'AONRD'[a^b::2]
-2 thanks to dingledooper
Anonymous function taking each truth value as a separate argument. Returns the concatenation of:
'XN'[a^d: ... ]
X, if the output for 11 is 0 (flipped if the output for 00 is 1);
'XN'[ ... :-~a]
N, if the output for 00 is 1;
'AONRD'[a^b::2]
and either AND or OR, depending on the output for 01 (flipped if the output for 00 is 1), by choosing either the even- or odd-indexed elements of AONRD.
