33
\$\begingroup\$

It seems we've managed to go all this time without a plain vanilla Number-To-Binary challenge! Whilst this will inevitably be only one element in many languages, it should put a few esolangs through their paces.

I truly looked for this challenge to no avail. If it already exists, comment as such and I'll delete this post-haste.

Input

A single non-negative integer.

Output

The same number as represented in Base 2.

Test Cases

  • 4 -> 100
  • 10 -> 1010
  • 1234 -> 10011010010

Victory Condition

, so fewest bytes wins!

Notes

\$\endgroup\$
9
  • 4
    \$\begingroup\$ Related, related. \$\endgroup\$
    – chunes
    Feb 27, 2023 at 3:27
  • 2
    \$\begingroup\$ @chunes Both are source restricted, which makes them different challenges \$\endgroup\$
    – ATaco
    Feb 27, 2023 at 3:36
  • 3
    \$\begingroup\$ Still it’s often helpful for golfers to know of related challenges. \$\endgroup\$
    – doug
    Feb 27, 2023 at 7:09
  • 3
    \$\begingroup\$ @KevinCruijssen Leading zeroes are fine, Trailing zeroes go without saying are not. \$\endgroup\$
    – ATaco
    Feb 27, 2023 at 8:26
  • 3
    \$\begingroup\$ Can we output the binary as little-endian? \$\endgroup\$
    – pajonk
    Mar 1, 2023 at 11:41

76 Answers 76

1 2
3
1
\$\begingroup\$

Simply, 17 bytes

The code simply creates an anonymous arrow function that calls the &tb function, and returns the result.

fn($x)=>&tb($x 2)

To use this function, assign it to a variable and call it:

$fn = fn($x)=>&tb($x 2)

echo call $fn(5);

What does &tb do?

The function &tb is a built-in alias to &tobase.

This is the result of running echo &doc(&tobase);:

Documentation for tobase:
Converts the integer $number to the $base.
The $base must be between 1 and 36, otherwise, returns null.
E.g.: tobase(10, 10) = "10", tobase(10, 2) = "1010", tobase(10, 1) = "1111111111".

Ungolfed

You can rewrite the code like this:

Set the variable $fn to an anonymous function with the argument $number.
Begin.
    Return the result of calling the function &tobase with the arguments $number and 2;
End.

Or, if you want it to be more code-like:

$fn = anonymous function($number) {
    return &tobase($number, 2);
};
\$\endgroup\$
1
\$\begingroup\$

Knight (v2), 29 bytes

;=a@;=n*2P;W=n/n 2=a+,%n 2aOa

Try it online!

Zero outputs nothing, which is allowed in the rules.

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

J 2 bytes

#:

Some test cases:

   #:12345

1 1 0 0 0 0 0 0 1 1 1 0 0 1

   #:33

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

Raku, 8 bytes

.base(2)

Try it online!

The above version operates on the current topic, as seen in the demo code. To make it a function accepting an argument, just wrap it in curly braces for another two bytes.

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

Thunno 2, 1 byte

Attempt This Online!

\$\endgroup\$
1
  • \$\begingroup\$ almost polyglots with Vyxal. Vyxal uses a b without the dot above. \$\endgroup\$ Jul 16, 2023 at 11:22
1
\$\begingroup\$

Zsh, 13 bytes

<<<$[[##2]$1]

Try it online!

Vanilla answer... here are a couple more interesting variants

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

ForWhile, 25 bytes

{:[:1&'2/0@1+0$:)@[48+#)}

Takes argument from the stack and prints to console

online interpreter

Explanation

{                       }  \ anonymous procedure
 :[                        \ repeat n times
   :1&                     \ push the lowest bit of n
      '2/                  \ divide n by 2
         0@1+0$            \ increment memory cell 0
               :)          \ break loop if n is zero
                 @         \ read memory at address 0 (n is still on stack)
                  [48+#)   \ print bits converted to characters 
\$\endgroup\$
1
\$\begingroup\$

Japt, 1 byte

¤

Try it

Shortcut for s2 which converts a number to a string in base 2

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

Uiua, 9 bytes SBCS

&pf⇌⋯⋕&sc

Try it online! Outputs as a list of bits.

Explanation

&pf⇌⋯⋕&sc
      &sc # read line from stdin
     ⋕    # parse string as number
    ⋯     # encode number as bit array, LSB-first
   ⇌      # reverse array
&pf       # print to stdout
\$\endgroup\$
0
\$\begingroup\$

Nibbles, 1.5 bytes

``@

Attempt This Online!

Built-in.

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

Nim, 45 bytes

import strformat
func b(n:int):auto= &"{n:b}"

Attempt This Online!

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

Risky, 1.5 bytes

?}2

Try it online!

?}2
 }  Convert
?   input
 }  to base
  2 2
\$\endgroup\$
0
\$\begingroup\$

flax, 1 byte

B

Attempt This Online!

Builtin

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

vemf, 1 byte

é

Represents α as a 64-element list of bits.

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

*><>, 14 bytes

Input is passed in through the -i flag.

:2%:n-2,:0)?!;
:               Duplicate the value on the stack
 2              Push 2 onto the stack
  %             Modulo those two
   :            Duplicate the result
    n           Pop and print that duplicate
     -          Subtract the result from our original number
      2         Push 2
       ,        Divide the two
        :       Duplicate that result
         0      Push 0
          )?!;  If the dividend is greater than 0, loop again

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ I just realized my output is in reverse 😑 If anyone has any ideas on how to get it in the correct order, I'd appreciate it \$\endgroup\$
    – Bee H.
    Feb 15 at 15:20
0
\$\begingroup\$

YASEPL, 76 72 bytes

=d'(=1)""`9!m$d%!1ſm!d/(}7,0,9=o-!h)""!u®1-=k$u`8!m¥u,1!hſm!u-}1,o,8>h

YASEPL is a (horribly coded and just stupid in general) esolang I made a while ago in Node.JS. it gets input via a prompt and STDIN. heres the output:

a picture containing the output of the code I just put.

\$\endgroup\$
1 2
3

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.