# Number to Binary

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

• The output must consist of digits 0 and 1, either as characters or individual numbers in a list.
• Zero may optionally return nothing.
• Standard IO Applies
• Standard Loopholes Apply
• Have Fun!
• Commented Feb 27, 2023 at 3:27
• @chunes Both are source restricted, which makes them different challenges Commented Feb 27, 2023 at 3:36
• Still it’s often helpful for golfers to know of related challenges.
– doug
Commented Feb 27, 2023 at 7:09
• @KevinCruijssen Leading zeroes are fine, Trailing zeroes go without saying are not. Commented Feb 27, 2023 at 8:26
• Can we output the binary as little-endian? Commented Mar 1, 2023 at 11:41

# 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); };  # 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. # 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  # 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. # Thunno 2, 1 byte ḃ  Attempt This Online! • almost polyglots with Vyxal. Vyxal uses a b without the dot above. Commented Jul 16, 2023 at 11:22 # Zsh, 13 bytes <<<$[[##2]$1]  Try it online! Vanilla answer... here are a couple more interesting variants # 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  # Japt, 1 byte ¤  Try it Shortcut for s2  which converts a number to a string in base 2 # 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  # Nibbles, 1.5 bytes @  Attempt This Online! Built-in. # Nim, 45 bytes import strformat func b(n:int):auto= &"{n:b}"  Attempt This Online! # Risky, 1.5 bytes ?}2  Try it online! ?}2 } Convert ? input } to base 2 2  # flax, 1 byte B  Attempt This Online! Builtin ## vemf, 1 byte é  Represents α as a 64-element list of bits. # *><>, 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! • 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 Commented Feb 15 at 15:20 # YASEPL, 76 72 bytes =d'(=1)""9!m$d%!1ſm!d/(}7,0,9=o-!h)""!u®1-=k\$u8!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: