| Bytes | Lang | Time | Link |
|---|---|---|---|
| 048 | AWK | 241104T174017Z | xrs |
| 073 | BrainChild | 241104T035931Z | ATaco |
| 002 | Japt | 230813T082925Z | Shaggy |
| 078 | brainfuck | 241103T192457Z | noodle p |
| 039 | YASEPL | 240207T203922Z | madeforl |
| 081 | Binary Lambda Calculus 81 bits = 10.125 bytes | 240412T125922Z | Legendar |
| 004 | Uiua | 240306T165821Z | noodle p |
| 022 | Kotlin | 240310T101707Z | Cactusro |
| 048 | Swift 5.9 | 240306T204114Z | macOSist |
| 025 | Scala 3 | 240306T173448Z | movatica |
| 046 | Thue | 230901T005749Z | MegaTom |
| 042 | BrainFlak | 230901T004716Z | MegaTom |
| 058 | C clang | 230811T173234Z | corvus_1 |
| 020 | Ruby | 230812T174621Z | kait0u |
| 024 | Julia 1.0 | 230816T152740Z | Ashlin H |
| 053 | brainfuck | 230815T200012Z | fwoosh |
| 028 | PowerShell | 230815T144606Z | James Fl |
| 017 | dc | 230814T161450Z | Digital |
| 028 | sed 4.2.2 | 230814T154002Z | Digital |
| 010 | x86+MMX machine code | 230812T211907Z | Peter Co |
| 033 | Lua | 230812T134614Z | val - di |
| 022 | R | 230811T212215Z | Dominic |
| 015 | Perl 5 | 230812T113035Z | Kjetil S |
| 026 | Haskell | 230811T210136Z | xnor |
| 015 | TIBasic | 230811T234118Z | Youserna |
| 079 | TypeScript Type System | 230811T230743Z | emanresu |
| 018 | /// | 230811T224118Z | emanresu |
| 003 | Husk | 230811T205751Z | Dominic |
| 003 | 05AB1E | 230811T194318Z | Kevin Cr |
| 045 | J | 230811T202358Z | south |
| 032 | PHP | 230811T201824Z | Samuel C |
| 003 | MathGolf | 230811T200116Z | Kevin Cr |
| 016 | Retina 0.8.2 | 230811T194127Z | Neil |
| 023 | Arturo | 230811T191419Z | chunes |
| 003 | Charcoal | 230811T173422Z | Neil |
| 128 | TypeScript's Type System | 230811T163219Z | noodle p |
| 021 | JavaScript Node.js | 230811T161754Z | l4m2 |
| 020 | Excel | 230811T165730Z | Jos Wool |
| 014 | Mathematica | 230811T163358Z | Michael |
| 021 | Python 3 | 230811T162005Z | The Thon |
| 002 | Vyxal | 230811T161621Z | The Thon |
| 002 | Thunno 2 | 230811T161134Z | The Thon |
| 003 | APL Dyalog Extended | 230811T160808Z | Adá |
AWK, 48 bytes
{gsub(1,"0*");while(sub(/*0/,"0**"));gsub(0,X)}1
Another version:
{for(;i++<=NF;)for(k=1;$i&&k++<=2^(NF-i);)printf "*"}
BrainChild, 73 bytes
include*;int j=0;gets()(function(int c)j=j*2+(c-48))while(j--)putchar(65)
Readable
include io.bc;
int j=0;
gets()(function(int c){
j=j*2;
j=j+(c-48)
})
while(j--)
putchar('A')
Japt, 2 bytes
Takes input as a binary string, outputs a string of spaces.
Íç
Try it (Footer replaces spaces with 1s)
Íç :Implicit input of binary string
Í :Convert to decimal
ç :Repeat a space that many times
brainfuck, 78 bytes
+>>,[>+++++++[-<------->]<+<<[->+>>++<<<]<++++++[->++++++<]>>>[-<[-<.>]>]>>>,]
Someone already did it in brainfuck and got a better score here, but I thought I'd try it out myself.
YASEPL, 65 61 49 47 45 41 39 bytes
=i=1'=l®1+`1--!a¥i,1=r$2^l*a;a,r~!+!l+[
1 is the unary digit, prompts you for input
YASEPL, 37 35 bytes
this one exits with an error but works the same.
=i=1'=l®1-i-!a¥i,1=r$2^l*a;a,r~!+?6
Binary Lambda Calculus: 81 bits = 10.125 bytes
Lambda calculus has no standard encoding for binary numbers. I chose the conversion from my favorite base 2 encoding to unary Church numerals.
Mogensen's general encoding for arbitrary base b is defined by
$$ \langle n\rangle_b=\lambda^{S(b)}(d_0\circ\dots\circ d_{\lfloor\log_b(n)\rfloor}\ b) $$ $$ \text{where de Bruijn index }\ d_i=\frac{n}{b^i}\pmod{b} $$
E.g. $$\langle6\rangle_2=\lambda^3(0\ (1\ (1\ 2)))$$
Conversion from b=2 to Church numerals follows by applying $$(\langle n\rangle_2\ \langle0\rangle_u\ (\mathrm{succ}_u\circ(\mathrm{mult}_u\ \langle2\rangle_u))\ (\mathrm{mult}_u\ \langle2\rangle_u))$$
Which golfs down to a function of 81 bits length:
000101011000001000000001110010111101100101111011010000000010111101100101111011010
I wasn't able to share the two curried multiplication functions completely since additional abstractions/indices actually increased the size. Maybe someone else here has some better ideas?
Trying-to-be-clever 84 bit alternative:
000100010101110000010000000011100101011111011101101010000000010111101100101111011010
Uiua, 6 4 bytes
⊚°⋯⇌
Convert from binary, use ⊚ where to change this to an array of N 0s.
Before I knew about °⋯:
Uiua, 13 bytes
×0⇡/+×ⁿ:2⇌⇡⧻.
Multiply by powers of 2, sum, range, multiply each by 0.
Swift 5.9, 48 bytes
let f={{String.init}()("*",Int($0+"",radix:2)!)}
Brain-Flak, 42 bytes
([]){{}({}<>({}){})<>([])}<>{({}[(())])}{}
The code works by accumulating the values of partial sums on the second stack, then it expands the second stack from the single value n, to n copies of 1.
([]) Push the stack height
{ While the stack is not empty
{} pop the stack height
(
{} pop the top of the stack
<> Jump to the second stack
({}){} Add the top element of the second stack twice, popping it
) Push the sum
<>
([]) Push new height
}
<> On the second stack:
{ While n>0
({} Pop n
[(()) Push 1
] add -1
) Push n-1
}{} Pop the zero
C (clang), 60 58 bytes
i;f(char*s){for(i=0;*++s;i+=i+*s-48);i&&printf("%0*d",i);}
Ruby, 20 bytes
->(b){?**b.to_i(2)}
It goes from binary to a decimal integer, and becomes a factor by which an asterisk char is multiplied.
(Idea to switch to char from string was suggested by Travis. Thanks!)
Ruby, 21 bytes | (old)
->(b){"*"*b.to_i(2)}
Same as the upper one, but the upper one has been improved.
Julia 1.0, 24 bytes
~x="*"^parse(Int,"0b"*x)
Generally, any base other than 10 needs to be specified: parse(Int,x,base=2). Two bytes are saved by appending a 0b integer prefix instead.
brainfuck, 53 bytes
Takes a string of 0 and 1 characters, outputs a string of 1s.
>,[>-[<->-----]<+++<[->++<]>>,]>-[<+>-----]<--<[->.<]
Explaination
>,[ read a character
>-[<->-----]<+++ subtract 48 to turn it into 0/1
<[->++<] double the existing sum and add it to the new digit
>>,] read another character and repeat if not EOF
>-[<+>-----]<-- make 49
<[->.<] print 49 the necessary number of times
dc, 17
[q]sz2iA?^9/d0=zn
Explanation
[ ] # define a macro to...
q # quit (i.e. print the empty string)
sz # save the macro to register z
2i # set input radix to 2 (binary)
A # pop 10
? # read input
^ # exponentiate 10 to the power of the input
9/ # divide by 9. This gives a number consisting of n 1s, or 0
d # duplicate top of stack
0=z # if equal to 0, call macro stored in z register to print the empty string
n # otherwise print the unary number
x86+MMX machine code, 10 bytes
Despite the question only saying the input has to be "binary", comments seem to imply they're thinking of a text string of base-2 digits, not 1-bit digits packed into an integer which a computer can already use directly as an unsigned integer. If that's allowed, see my 7-byte answer on Output / Convert to unary number.
This is that answer plus conversion from 8 bytes to an 8-bit integer, by extracting the high bit of each input. Input is in mm0, an MMX vector register, with the lowest element being the least significant. (If stored to memory, this would be opposite of standard printing order where the most significant goes first, at the lowest address of a string.)
; machine code | NASM source
; RDI or ES:EDI = output buffer of size n+1
binary_to_unary:
; pslld mm0, 7 ; 4 bytes. For input digits '0' / '1', shift the low bit to high
0FD7C8 pmovmskb ecx, mm0 ; pack the binary digits into an integer in ECX
; memset(dst, '1', n)
B031 mov al, '1'
F3AA rep stosb
880F mov byte [rdi], cl ; terminate the C-style string
C3 ret
For ASCII '0' / '1' input, we'd start with pslld mm0, 7 to shift the low bit to the high position of each byte. Also, if the digit-string was loaded from a string in memory in printing order (most significant at lowest address), we'd need to byte-reverse. In that case scalar code to read input would be smaller than movbe or load+bswap in an integer register + movq mm0, rax, or psufb with an 8-byte vector constant.
In this current version, our input string in an MMX register is effectively big-endian, most significant digit in the highest element number (on the left when writing it in the notation Intel manuals use to document vector instructions).
x86 scalar machine code, 14 bytes, input from a C string in printing order
- RDI or ES:EDI points to an input buffer of base-2 ASCII digits, terminated by a
0byte (or any byte below ASCII'0'), padded to at least 32 digits. - RSI or ES:ESI points to an output buffer of size n+1, will be filled with
'0'digits and terminated with a0.
Callable from C with the x86-64 System V calling convention as void binary_string_to_unary(const char *binary_src, char *unary_dst);. Use the x32 ABI for 32-bit pointers, or change the mov edi, esi to push rsi/pop rdi to copy a 64-bit register with the same code size. (Or use the same binary machine code in 32-bit mode.)
binary_string_to_unary:
B030 mov al, '0' ; input digit to compare against, also the output digit
.loop:
11C9 adc ecx, ecx ; ECX = 2*ECX + CF
AE scasb ; set FLAGS like cmp al, [rdi] then increment RDI. CF=1 for digit='1', CF=0 for digit='0'.
76FB jbe .loop ; while('0' <= digit); i.e. while digit >= '0', false for digit = 0 terminator
89F7 mov edi, esi ; x32 ABI, or for 32-bit mode. For full 64-bit pointers, push rsi / pop rdi
F3AA rep stosb ; memset(rdi, al='0', rcx)
880F mov [rdi], cl ; terminate the C-style string
C3 ret
Note that we don't xor ecx,ecx before the loop. Instead we rely on the input string being '0'-padded to at least 32 digits, so the previous contents get shifted out.
There might be a way (with same code size) to take the input pointer in RSI like you'd expect from the traditional meanings of those registers, perhaps with lodsb / cmp al, '0' (3 bytes instead of 1 for scasb, but avoiding the 2-byte mov edi, esi after the loop). Change the branch conditions accordingly for comparing in the other direction. Except that would get CF=0 for digit='1'. So maybe add al, 255-'0' so '1' produces a carry-out but '0' doesn't? And the terminator is a value that results in some FLAGS condition that the other two don't, perhaps a signed overflow and/or the sign flag.
R, 22 bytes
\(x)rep(1,strtoi(x,2))
Convert from binary and repeat 1 that many times.
Unfortunately shorter than the more-interesting (I think):
R, 36 34 bytes
Edit: -2 bytes thanks to pajonk
\(x)Reduce(\(a,b)c(a,a,b[b]),x,{})
Input is a vector of binary digits; fold over (Reduce) this from the left, starting with an empty vector (NULL), at each step duplicating whatever we've got so far, and appending the new digit if it's a 1.
Perl 5, 15 bytes
$_=9x oct"0b$_"
Outputs 9 (my chosen char) the number of times given in the input binary number.
Haskell, 26 bytes
f l=10^foldl((+).(2*))0l-1
Input is a list of 0/1, output is a number like 9999999999 made of the digit 9.
Haskell is interesting for this challenge because it lacks base-conversion built-ins without imports, so we have to do it ourselves. This answer folds the operation \n b->n*2+b over the list of bits to get a number k, then computes 10^k-1 to get a number made of k 9-digits. The 9's trick is shorter than more natural methods like replicate k 1 or 1<$[1..k] or take k[0,0..].
It's slightly longer to do the power-of-10 within the fold and decrement after:
27 bytes
pred.foldl(\n b->n^2*10^b)1
though if we bend the rules and represent bits as 1 or decimal 10, we can do
21 bytes
pred.foldl((*).(^2))1
Haskell, 26 bytes
foldl(\s b->s++s++[1|b])[]
Input is list of Booleans, output is a list of 1's. This iterates over the list of Booleans, each time doubling the current string then appending an additional 1 if the bit is True.
TI-Basic, 15 bytes
1 or rand(.5sum(Ans2^cumSum(1 or Ans
Input is taken as a list of 0s and 1s in little-endian binary in Ans. Output is a list of 1s.
TypeScript Type System, 79 bytes
type B<S>=S extends`${infer Q}${infer V}`?`${B<V>}${B<V>}${Q extends"1"?Q:""}`:S
Try it on the Typescript Playground!
Note: This works with little-endian binary, not big-endian.
While the algorithm in the post allows simply performing replacements, we can take advantage of Typescript's ability to anchor string matches for a much simpler algorithm.
Essentially, S extends`${infer Q}${infer V}` matches the first character Q and the rest of the string V. We recurse on V twice, and append a 1 if Q is a 1.
///, 18 bytes
/1/0*//*0/0**//0//
I didn't write this, I borrowed it from the esolangs wiki. Here's how it works:
Let's start with the string 10101, which represents 21.
/1/0*/ replaces 1s with 0*. The string becomes 0*00*00*. In this, each * represents a sort of place value - for example, the first * has 4 0s after it, so it represents 24 = 16.
/*0/0**/ replaces each * before a zero with two copies of it after the zero, each with half the place value. The first replacement this would do would be replacing 0*00*00* with 00**0*00*, replacing a * representing 16 with 2 *s each representing 8.
In ///, replacements are repeated until nothing is left to replace, so this will eventually result in a string with some amount of 0s at the start, followed by 21 *s each representing 1.
Finally, /0// removes the leading zeros, leaving just 21 *s, which is the output in unary.
05AB1E, 3 bytes
Since outputting the unary as a single character is required, there are a couple of 3 bytes alternatives:
- Output as spaces:
Cð×- Try it online or verify all test cases; - Output as 1s:
$C×- Try it online or verify all test cases; - Output as 0s:
ÎC×- Try it online or verify all test cases; - Output as any digit (as a list of characters):
CÅ9(where the9could be any other digit) - Try it online or verify all test cases.
If a mixture of multiple characters for the unary output would have been allowed, since only the length matters in unary, it could have been 2 bytes with:
C∍
Outputs a mixture of 1s and 0s (depending on the input) for its unary output.
Try it online or verify all test cases.
Explanation:
C # Convert the (implicit) input binary-string to a base-10 integer
ð× # Pop and push a string with that many spaces
# (after which the result is output implicitly)
$ # Push 1 and the input binary-stringC×
Î # Or alternatively, push 0 and the input binary-string
C # Convert the binary-string to a base-10 integer
× # Repeat the 1 or 0 that many times as string
# (after which the result is output implicitly)
C # Convert the (implicit) input binary-string to a base-10 integer
Å9 # Pop and push a list with that many 9s (or any other digit)
# (after which the result is output implicitly)
C # Convert the (implicit) input binary-string to a base-10 integer
∍ # Extend/shorten the (implicit) input binary-string to that length
# (after which the result is output implicitly)
J, 4 or 5 bytes
#.##
Uses the length of the input digit list for the unary digit.
Like 05AB1E, uniformity is one extra byte, with alternatives.
MathGolf, 3 bytes
Some different alternatives are available:
- Output as
*s:å⌂*;åÄ⌂;å{⌂. - Output as spaces:
å *;åÄ;å{. - All six above should have been also possible with a binary-list instead of binary-string as input, replacing the
åwithä, but unfortunately there seems to be a bug with leading0s apparently.. - Tryä⌂*online.
Explanation:
å # Convert the (implicit) input binary-string to a base-10 integer
⌂* # Repeat that many "*" as string
* # Or alternatively, repeat that many spaces as string
# (after which the entire stack is output implicitly as result)
å # Convert the (implicit) input binary-string to a base-10 integer
Ä # Pop and loop that many times, using the following character as inner code-block:
{ # Or alternatively, use everything after it as code-block:
⌂ # Push an "*" to the stack every iteration
# Or alternatively, push a space character to the stack every iteration
# (after which the entire stack is joined together and output implicitly as result)
Retina 0.8.2, 16 bytes
1
01
+`10
011
0
Try it online! Link includes test cases. Explanation: A direct interpretation of the method given in the question, except using 1 instead of * for obvious reasons. Note that the method in Retina's own tutorial is slightly different preferring to use ^0+ presumably for a slight performance improvement.
Arturo, 23 bytes
$=>[@.of:from.binary&1]
Returns a list of digits.
$=>[ ; a function where input is assigned to &
from.binary& ; convert input from binary string to decimal integer
@.of: _ 1 ; create list of that many 1s
] ; end function
Charcoal, 3 bytes
⍘S²
Try it online! Link is to verbose version of code. Outputs a string of -s. Explanation:
S Input string
⍘ Convert from base
² Literal integer `2`
Print as row of `-`s
Unfortunately BaseString(2, InputString()) performs a different operation, which means I can't use implicit input to save a byte.
TypeScript's Type System, 130 128 bytes
//@ts-ignore
type R<S,F,X>=S extends`${infer A}${F}${infer B}`?R<`${A}${X}${B}`,F,X>:S;type B<S>=R<R<R<S,1,"02">,20,"022">,0,"">
Try it at the TypeScript Playground!
Uses 2 as the unary digit. Defines a utility type to recursively remove all occurrences of a given string with a different string, and then defines the main type which is a series of nested calls to the utility type.
Excel, 20 bytes
=REPT(9,BIN2DEC(A1))
Input in cell A1.
Mathematica, 14 bytes
As a list of '1's:
Table[1,2^^#] &
Anonymous function. Would probably be 10 bytes in the hypothetical mthmca golfing language.
Vyxal, 2 bytes
BI
Try it Online! or see it with asterisks instead
Same as my Thunno 2 answer.
Explanation
BI # Implicit input
B # From binary
I # That many spaces
# Implicit output
Thunno 2, 2 bytes
Ḃṣ
Try it online! or see it with asterisks instead
Uses spaces.
Explanation
Ḃṣ # Implicit input
Ḃ # From binary
ṣ # That many spaces
# Implicit output
APL (Dyalog Extended), 3 bytes
Anonymous tacit prefix function. Takes list of binary digits and returns list of 1s
⊥⍴≡
⊥ the base-2 evaluation
⍴ reshapes
≡ the depth (nesting level, which is always 1 for a list)