2
\$\begingroup\$

There are 10 types of people in the world, those who understand binary and those who do not.

Where are you? Let's see!

Challenge

Your task is to convert a decimal integer to binary, only using a set of arithmetic operators (+, -, *, / and %). You can use conditional statements, loops and variables. (No bitwise operator/No Builtin methods or function).

Input

A decimal integer.

Output

The binary number.

Examples

2 -> 10
6 -> 110
etc...

The shortest code wins.

\$\endgroup\$
  • 1
    \$\begingroup\$ I take it that / is integer (floored) division? When you say we can use conditional statements, can these include operators like == and >? \$\endgroup\$ – xnor Mar 11 at 8:49
  • 7
    \$\begingroup\$ Honestly, I think the only way to unambiguously define what constructs are allowed would be to fully specify your own mini-language, defining all operators within it and how they work , and require that answers be in it, atomic-code-golf-style. \$\endgroup\$ – xnor Mar 11 at 8:53
  • 3
    \$\begingroup\$ Is concatenation operator allowed? If not I'll delete my answer.. \$\endgroup\$ – Kaddath Mar 11 at 11:28
  • 3
    \$\begingroup\$ What counts as a loop? Does map or reduce? If I have an operator that takes a start point, transition and a condition and repeats the transition until the condition is met, is that a loop? I interpreted the spec as to mean bitwise operators, built-in methods or functions are banned, but everything else is fine, but other people have interpreted it as only arithmetic operators, conditional statements, loops and variables are allowed. Which one is it? \$\endgroup\$ – Jo King Mar 12 at 2:56
  • 2
    \$\begingroup\$ Is assignment not allowed (rules out recursive functions)? How about increment (++)? Are numeric literals banned? How can I do IO if I can't use the input/output commands? Are non-pure arithmetic operators (like +=) allowed? Honestly, I think there's more than enough ambiguities to be reclosed as unclear. \$\endgroup\$ – Jo King Mar 12 at 11:12

15 Answers 15

9
\$\begingroup\$

Python 2, 30 bytes

f=lambda n:n and n%2+10*f(n/2)

Try it online!

|improve this answer|||||
\$\endgroup\$
5
\$\begingroup\$

C (gcc), 50 27 26 bytes

f(n){n=n?n%2+10*f(n/2):n;}

Try it online!

|improve this answer|||||
\$\endgroup\$
5
\$\begingroup\$

Whitespace, 141 bytes

[S S S T    N
_Push_1][S N
S _Dupe_1][T    N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input][S N
S _Dupe_input][N
S S N
_Create_Label_LOOP][S N
S _Dupe_top][N
T   S S N
_If_0_Jump_to_Label_LOOP2][S S S T  N
_Push_1][T  S S T   _Subtract][S N
T   _Swap_top_two][S N
S _Dupe_top][S S S T    S N
_Push_2][T  S T T   _Modulo][S N
T   _Swap_top_two][S S S T  S N
_Push_2][T  S T S _Integer_divide][S T  S S T   S N
_Copy_0-based_2nd][N
S N
N
_Jump_to_Label_LOOP][N
S S S N
_Create_Label_LOOP2][S N
T   _swap_top_two][S S S T  N
_Push_1][S N
S _Dupe_1][T    T   T   _Retrieve_input][T  S S T   _Subtract][T    S S S _Add][N
T   S T N
_If_0_Jump_to_Label_DONE][S S S T   S T S N
_Push_10][T S S N
_Multiply][T    S S S _Add][N
S N
S N
_Jump_to_Label_LOOP2][N
S S T   N
_Create_Label_DONE][T   N
S T _Print_as_integer_to_STDOUT]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online (with raw spaces, tabs and new-lines only).

Funny thing is, is that I can use everything Whitespace has to offer for this challenge. So even if builtins were allowed, this would still be the approach to use, since Whitespace has none. ;)

Explanation in pseudo-code:

Integer input = read STDIN as integer
Integer index = input
Integer b = input
Start LOOP:
  If(index == 0):
    Jump to LOOP2
  index = index - 1
  Integer a[index] = b[previous_index] modulo-2
  Integer b[index] = b[previous_index] integer-divided by 2
  Go to next iteration of LOOP

Start LOOP2:
  If(index+1 == input):
    Jump to DONE
  a = a + b*10
  Go to next iteration of LOOP2

Label DONE:
  Print a as integer to STDOUT
  (stop program implicitly with an error)

Example run: input = 6

Command  Explanation                 Stack                             Heap  STDIN STDOUT

SSST     Push 1                      [1]
SNS      Duplicate top (1)           [1,1]
TNTT     Read STDIN as integer       [1]                               {1:6} 6
TTT      Retrieve input from heap    [6]                               {1:6}
SNS      Duplicate top (6)           [6,6]                             {1:6}
NSSN     Create Label LOOP           [6,6]                             {1:6}
 SNS      Duplicate top (6)          [6,6,6]                           {1:6}
 NTSSN    If 0: Jump to Label LOOP2  [6,6]                             {1:6}
 SSSTN    Push 1                     [6,6,1]                           {1:6}
 TSST     Subtract (6-1)             [6,5]                             {1:6}
 SNT      Swap top two               [5,6]                             {1:6}
 SNS      Duplicate top (6)          [5,6,6]                           {1:6}
 SSSTSN   Push 2                     [5,6,6,2]                         {1:6}
 TSTT     Modulo (6%2)               [5,6,0]                           {1:6}
 SNT      Swap top two               [5,0,6]                           {1:6}
 SSSTSN   Push 2                     [5,0,6,2]                         {1:6}
 TSTS     Integer-divide (6//2)      [5,0,3]                           {1:6}
 STSSTSN  Copy 0-based 2nd (5)       [5,0,3,5]                         {1:6}
 NSNN     Jump to Label LOOP         [5,0,3,5]                         {1:6}

 SNS      Duplicate top (5)          [5,0,3,5,5]                       {1:6}
 NTSSN    If 0: Jump to Label LOOP2  [5,0,3,5]                         {1:6}
 SSSTN    Push 1                     [5,0,3,5,1]                       {1:6}
 TSST     Subtract (5-1)             [5,0,3,4]                         {1:6}
 SNT      Swap top two               [5,0,4,3]                         {1:6}
 SNS      Duplicate top (3)          [5,0,4,3,3]                       {1:6}
 SSSTSN   Push 2                     [5,0,4,3,3,2]                     {1:6}
 TSTT     Modulo (3%2)               [5,0,4,3,1]                       {1:6}
 SNT      Swap top two               [5,0,4,1,3]                       {1:6}
 SSSTSN   Push 2                     [5,0,4,1,3,2]                     {1:6}
 TSTS     Integer-divide (3//2)      [5,0,4,1,1]                       {1:6}
 STSSTSN  Copy 0-based 2nd (4)       [5,0,4,1,1,4]                     {1:6}
 NSNN     Jump to Label LOOP         [5,0,4,1,1,4]                     {1:6}

 SNS      Duplicate top (4)          [5,0,4,1,1,4,4]                   {1:6}
 NTSSN    If 0: Jump to Label LOOP2  [5,0,4,1,1,4]                     {1:6}
 SSSTN    Push 1                     [5,0,4,1,1,4,1]                   {1:6}
 TSST     Subtract (4-1)             [5,0,4,1,1,3]                     {1:6}
 SNT      Swap top two               [5,0,4,1,3,1]                     {1:6}
 SNS      Duplicate top (1)          [5,0,4,1,3,1,1]                   {1:6}
 SSSTSN   Push 2                     [5,0,4,1,3,1,1,2]                 {1:6}
 TSTT     Modulo (3%2)               [5,0,4,1,3,1,1]                   {1:6}
 SNT      Swap top two               [5,0,4,1,3,1,1]                   {1:6}
 SSSTSN   Push 2                     [5,0,4,1,3,1,1,2]                 {1:6}
 TSTS     Integer-divide (1//2)      [5,0,4,1,3,1,0]                   {1:6}
 STSSTSN  Copy 0-based 2nd (3)       [5,0,4,1,3,1,0,3]                 {1:6}
 NSNN     Jump to Label LOOP         [5,0,4,1,3,1,0,3]                 {1:6}

 SNS      Duplicate top (3)          [5,0,4,1,3,1,0,3,3]               {1:6}
 NTSSN    If 0: Jump to Label LOOP2  [5,0,4,1,3,1,0,3]                 {1:6}
 SSSTN    Push 1                     [5,0,4,1,3,1,0,3,1]               {1:6}
 TSST     Subtract (3-1)             [5,0,4,1,3,1,0,2]                 {1:6}
 SNT      Swap top two               [5,0,4,1,3,1,2,0]                 {1:6}
 SNS      Duplicate top (0)          [5,0,4,1,3,1,2,0,0]               {1:6}
 SSSTSN   Push 2                     [5,0,4,1,3,1,2,0,0,2]             {1:6}
 TSTT     Modulo (0%2)               [5,0,4,1,3,1,2,0,0]               {1:6}
 SNT      Swap top two               [5,0,4,1,3,1,2,0,0]               {1:6}
 SSSTSN   Push 2                     [5,0,4,1,3,1,2,0,0,2]             {1:6}
 TSTS     Integer-divide (0//2)      [5,0,4,1,3,1,2,0,0]               {1:6}
 STSSTSN  Copy 0-based 2nd (2)       [5,0,4,1,3,1,2,0,0,2]             {1:6}
 NSNN     Jump to Label LOOP         [5,0,4,1,3,1,2,0,0,2]             {1:6}

 SNS      Duplicate top (2)          [5,0,4,1,3,1,2,0,0,2,2]           {1:6}
 NTSSN    If 0: Jump to Label LOOP2  [5,0,4,1,3,1,2,0,0,2]             {1:6}
 SSSTN    Push 1                     [5,0,4,1,3,1,2,0,0,2,1]           {1:6}
 TSST     Subtract (2-1)             [5,0,4,1,3,1,2,0,0,1]             {1:6}
 SNT      Swap top two               [5,0,4,1,3,1,2,0,1,0]             {1:6}
 SNS      Duplicate top (0)          [5,0,4,1,3,1,2,0,1,0,0]           {1:6}
 SSSTSN   Push 2                     [5,0,4,1,3,1,2,0,1,0,0,2]         {1:6}
 TSTT     Modulo (0%2)               [5,0,4,1,3,1,2,0,1,0,0]           {1:6}
 SNT      Swap top two               [5,0,4,1,3,1,2,0,1,0,0]           {1:6}
 SSSTSN   Push 2                     [5,0,4,1,3,1,2,0,1,0,0,2]         {1:6}
 TSTS     Integer-divide (0//2)      [5,0,4,1,3,1,2,0,1,0,0]           {1:6}
 STSSTSN  Copy 0-based 2nd (1)       [5,0,4,1,3,1,2,0,1,0,0,1]         {1:6}
 NSNN     Jump to Label LOOP         [5,0,4,1,3,1,2,0,1,0,0,1]         {1:6}

 SNS      Duplicate top (1)          [5,0,4,1,3,1,2,0,1,0,0,1,1]       {1:6}
 NTSSN    If 0: Jump to Label LOOP2  [5,0,4,1,3,1,2,0,1,0,0,1]         {1:6}
 SSSTN    Push 1                     [5,0,4,1,3,1,2,0,1,0,0,1,1]       {1:6}
 TSST     Subtract (1-1)             [5,0,4,1,3,1,2,0,1,0,0,0]         {1:6}
 SNT      Swap top two               [5,0,4,1,3,1,2,0,1,0,0,0]         {1:6}
 SNS      Duplicate top (0)          [5,0,4,1,3,1,2,0,1,0,0,0,0]       {1:6}
 SSSTSN   Push 2                     [5,0,4,1,3,1,2,0,1,0,0,0,0,2]     {1:6}
 TSTT     Modulo (0%2)               [5,0,4,1,3,1,2,0,1,0,0,0,0]       {1:6}
 SNT      Swap top two               [5,0,4,1,3,1,2,0,1,0,0,0,0]       {1:6}
 SSSTSN   Push 2                     [5,0,4,1,3,1,2,0,1,0,0,0,0,2]     {1:6}
 TSTS     Integer-divide (0//2)      [5,0,4,1,3,1,2,0,1,0,0,0,0]       {1:6}
 STSSTSN  Copy 0-based 2nd (0)       [5,0,4,1,3,1,2,0,1,0,0,0,0,0]     {1:6}
 NSNN     Jump to Label LOOP         [5,0,4,1,3,1,2,0,1,0,0,0,0,0]     {1:6}

 SNS      Duplicate top (1)          [5,0,4,1,3,1,2,0,1,0,0,0,0,0,0]   {1:6}
 NTSSN    If 0: Jump to Label LOOP2  [5,0,4,1,3,1,2,0,1,0,0,0,0,0]     {1:6}
NSSSN    Create Label LOOP2          [5,0,4,1,3,1,2,0,1,0,0,0,0,0]     {1:6}
 SNT      Swap top two               [5,0,4,1,3,1,2,0,1,0,0,0,0,0]     {1:6}
 SSSTN    Push 1                     [5,0,4,1,3,1,2,0,1,0,0,0,0,0,1]   {1:6}
 SNS      Duplicate top (1)          [5,0,4,1,3,1,2,0,1,0,0,0,0,0,1,1] {1:6}
 TTT      Retrieve input from heap   [5,0,4,1,3,1,2,0,1,0,0,0,0,0,1,6] {1:6}
 TSST     Subtract (1-6)             [5,0,4,1,3,1,2,0,1,0,0,0,0,0,-5]  {1:6}
 TSSS     Add (0+-5)                 [5,0,4,1,3,1,2,0,1,0,0,0,0,-5]    {1:6}
 NTSTN    If 0: Jump to Label DONE   [5,0,4,1,3,1,2,0,1,0,0,0,0]       {1:6}
 SSSTSTSN Push 10                    [5,0,4,1,3,1,2,0,1,0,0,0,0,10]    {1:6}
 TSSN     Multiply (0*10)            [5,0,4,1,3,1,2,0,1,0,0,0,0]       {1:6}
 TSSS     Add (0+0)                  [5,0,4,1,3,1,2,0,1,0,0,0]         {1:6}
 NSNSN    Jump to Label LOOP2        [5,0,4,1,3,1,2,0,1,0,0,0]         {1:6}

 SNT      Swap top two               [5,0,4,1,3,1,2,0,1,0,0,0]         {1:6}
 SSSTN    Push 1                     [5,0,4,1,3,1,2,0,1,0,0,0,1]       {1:6}
 SNS      Duplicate top (1)          [5,0,4,1,3,1,2,0,1,0,0,0,1,1]     {1:6}
 TTT      Retrieve input from heap   [5,0,4,1,3,1,2,0,1,0,0,0,1,6]     {1:6}
 TSST     Subtract (1-6)             [5,0,4,1,3,1,2,0,1,0,0,0,-5]      {1:6}
 TSSS     Add (0+-5)                 [5,0,4,1,3,1,2,0,1,0,0,-5]        {1:6}
 NTSTN    If 0: Jump to Label DONE   [5,0,4,1,3,1,2,0,1,0,0]           {1:6}
 SSSTSTSN Push 10                    [5,0,4,1,3,1,2,0,1,0,0,10]        {1:6}
 TSSN     Multiply (0*10)            [5,0,4,1,3,1,2,0,1,0,0]           {1:6}
 TSSS     Add (0+0)                  [5,0,4,1,3,1,2,0,1,0]             {1:6}
 NSNSN    Jump to Label LOOP2        [5,0,4,1,3,1,2,0,1,0]             {1:6}

 SNT      Swap top two               [5,0,4,1,3,1,2,0,0,1]             {1:6}
 SSSTN    Push 1                     [5,0,4,1,3,1,2,0,0,1,1]           {1:6}
 SNS      Duplicate top (1)          [5,0,4,1,3,1,2,0,0,1,1,1]         {1:6}
 TTT      Retrieve input from heap   [5,0,4,1,3,1,2,0,0,1,1,6]         {1:6}
 TSST     Subtract (1-6)             [5,0,4,1,3,1,2,0,0,1,-5]          {1:6}
 TSSS     Add (1+-5)                 [5,0,4,1,3,1,2,0,0,-4]            {1:6}
 NTSTN    If 0: Jump to Label DONE   [5,0,4,1,3,1,2,0,0]               {1:6}
 SSSTSTSN Push 10                    [5,0,4,1,3,1,2,0,0,10]            {1:6}
 TSSN     Multiply (0*10)            [5,0,4,1,3,1,2,0,0]               {1:6}
 TSSS     Add (0+0)                  [5,0,4,1,3,1,2,0]                 {1:6}
 NSNSN    Jump to Label LOOP2        [5,0,4,1,3,1,2,0]                 {1:6}

 SNT      Swap top two               [5,0,4,1,3,1,0,2]                 {1:6}
 SSSTN    Push 1                     [5,0,4,1,3,1,0,2,1]               {1:6}
 SNS      Duplicate top (1)          [5,0,4,1,3,1,0,2,1,1]             {1:6}
 TTT      Retrieve input from heap   [5,0,4,1,3,1,0,2,1,6]             {1:6}
 TSST     Subtract (1-6)             [5,0,4,1,3,1,0,2,-5]              {1:6}
 TSSS     Add (2+-5)                 [5,0,4,1,3,1,0,-3]                {1:6}
 NTSTN    If 0: Jump to Label DONE   [5,0,4,1,3,1,0]                   {1:6}
 SSSTSTSN Push 10                    [5,0,4,1,3,1,0,10]                {1:6}
 TSSN     Multiply (0*10)            [5,0,4,1,3,1,0]                   {1:6}
 TSSS     Add (1+0)                  [5,0,4,1,3,1]                     {1:6}
 NSNSN    Jump to Label LOOP2        [5,0,4,1,3,1]                     {1:6}

 SNT      Swap top two               [5,0,4,1,1,3]                     {1:6}
 SSSTN    Push 1                     [5,0,4,1,1,3,1]                   {1:6}
 SNS      Duplicate top (1)          [5,0,4,1,1,3,1,1]                 {1:6}
 TTT      Retrieve input from heap   [5,0,4,1,1,3,1,6]                 {1:6}
 TSST     Subtract (1-6)             [5,0,4,1,1,3,-5]                  {1:6}
 TSSS     Add (3+-5)                 [5,0,4,1,1,-2]                    {1:6}
 NTSTN    If 0: Jump to Label DONE   [5,0,4,1,1]                       {1:6}
 SSSTSTSN Push 10                    [5,0,4,1,1,10]                    {1:6}
 TSSN     Multiply (1*10)            [5,0,4,1,10]                      {1:6}
 TSSS     Add (1+10)                 [5,0,4,11]                        {1:6}
 NSNSN    Jump to Label LOOP2        [5,0,4,11]                        {1:6}

 SNT      Swap top two               [5,0,11,4]                        {1:6}
 SSSTN    Push 1                     [5,0,11,4,1]                      {1:6}
 SNS      Duplicate top (1)          [5,0,11,4,1,1]                    {1:6}
 TTT      Retrieve input from heap   [5,0,11,4,1,6]                    {1:6}
 TSST     Subtract (1-6)             [5,0,11,4,-5]                     {1:6}
 TSSS     Add (4+-5)                 [5,0,11,-1]                       {1:6}
 NTSTN    If 0: Jump to Label DONE   [5,0,11]                          {1:6}
 SSSTSTSN Push 10                    [5,0,11,10]                       {1:6}
 TSSN     Multiply (11*10)           [5,0,110]                         {1:6}
 TSSS     Add (1+10)                 [5,110]                           {1:6}
 NSNSN    Jump to Label LOOP2        [5,110]                           {1:6}

 SNT      Swap top two               [110,5]                           {1:6}
 SSSTN    Push 1                     [110,5,1]                         {1:6}
 SNS      Duplicate top (1)          [110,5,1,1]                       {1:6}
 TTT      Retrieve input from heap   [110,5,1,6]                       {1:6}
 TSST     Subtract (1-6)             [110,5,-5]                        {1:6}
 TSSS     Add (5+-5)                 [110,0]                           {1:6}
 NTSTN    If 0: Jump to Label DONE   [110]                             {1:6}
NSSTN    Create Label DONE           [110]                             {1:6}
 TNST     Print as integer to STDOUT []                                {1:6}      110

Stops with an error after printing the result, because no exit is defined.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Thanks but I will appreciate it more if you can somehow less the byte counter. Other ones are quite short. \$\endgroup\$ – Wasif Hasan Mar 12 at 16:34
  • \$\begingroup\$ @WasifHasan You can't really compare answers across different answers tbh. Whitespace simply is pretty verbose, since it has limited amount of builtins for the stack manipulation. I'm sure it can be slightly shorter somehow, but I don't think below 100 bytes is possible. To give another example: taking an input through STDIN/system-args, converting it to an integer, checking if that integer is a prime, and then outputting that boolean to STDOUT is 1 byte in 05AB1E, but 108 bytes in Java. \$\endgroup\$ – Kevin Cruijssen Mar 12 at 17:07
  • \$\begingroup\$ Thanks I just do not know Whitspace (Pls don't mind) \$\endgroup\$ – Wasif Hasan Mar 12 at 17:08
  • \$\begingroup\$ @WasifHasan Np, I was mainly trying to explain why some languages are longer than others. :) And you can click on the link in my header to see the Whitespace tutorial and see the limited amount of builtins available in the language. \$\endgroup\$ – Kevin Cruijssen Mar 12 at 17:10
4
\$\begingroup\$

05AB1E, 11 bytes

F2‰`s}΃T*+

Fixed with help of @Grimmy.

Try it online or verify the first \$[0,25]\$ inputs.

Explanation:

F       # Loop the (implicit) input-integer amount of times:
 2‰     #  Take the divmod-2 ([n//2, n%2]) of the top value on the stack
        #  (which will be the (implicit) input-integer in the very first iteration)
   `s   #  Push them separated to the stack, and swap their order
}Î      # After the loop: push 0 and the input to the stack
  ƒ     #  Loop the input+1 amount of times:
   T*   #  Multiply the top value by 10
     +  #  And add the top two values together
        # (after which it is output implicitly as result)
|improve this answer|||||
\$\endgroup\$
  • 1
    \$\begingroup\$ J isn't an arithmetic operator, conditional statement, loop or variable. I think you need to do something like this, which as a bonus doesn't leave leading 0's. \$\endgroup\$ – Grimmy Mar 11 at 9:54
  • \$\begingroup\$ @Grimmy Ah, you're completely right. J isn't allowed.. Thanks for the fix! \$\endgroup\$ – Kevin Cruijssen Mar 11 at 10:00
4
\$\begingroup\$

Perl 5 -Minteger -p, 32 bytes

$\+=$_%2*10**$i++,$_/=2while$_}{

Try it online!

Uses no concatenation. If concatenation is allowed, it would get a little shorter:

26 bytes

$\=$_%2 .$\,$_/=2while$_}{

Try it online!

|improve this answer|||||
\$\endgroup\$
4
\$\begingroup\$

PHP, 45 44 43 bytes

for(;$a=&$argn>=1;$a/=2)$n=$a%2 .$n;echo$n;

Try it online!

As the output is a string, it supports up to PHP_INT_MAX integers

Thanks to @MariaMiller for saving 1 byte!

Thanks to @GuillermoPhillips for the reference assignment trick!

|improve this answer|||||
\$\endgroup\$
  • 2
    \$\begingroup\$ 44 bytes (-1 byte) \$\endgroup\$ – Maria Miller Mar 12 at 3:36
  • \$\begingroup\$ @MariaMiller nice trick, thanks! \$\endgroup\$ – Kaddath Mar 12 at 8:02
  • 1
    \$\begingroup\$ One for 43 bytes \$\endgroup\$ – Guillermo Phillips Mar 12 at 9:47
  • \$\begingroup\$ @GuillermoPhillips thanks! I had thought about a way not to reset $a each loop turn but didn't think about reference.. just hope it doesn't count as an invalid operator :D \$\endgroup\$ – Kaddath Mar 12 at 10:07
4
\$\begingroup\$

Ruby, 26 bytes

f=->n{n>0?n%2+10*f[n/2]:n}

Try it online!

|improve this answer|||||
\$\endgroup\$
3
\$\begingroup\$

MathGolf, 12 bytes

Longer than 05AB1E because MathGolf doesn't have divmod ...

_{2‼%/}k{♂*+

Try it online!

Explanation

_            We have to copy the input because
             MathGolf outputs 0 over an empty stack.
 {    }      Do that input times:
   ‼%/       "Moddiv"
  2          by 2 (This saves the swapping of the stack)
       k     Repeat input times:
        {♂*  Multiply TOS by 10
           + And add second-to-top

MathGolf, 10 bytes

If only concatenation is allowed. MathGolf cleverly avoids the leading zeroes, unlike 05AB1E which doesn't.

_{2‼%/}]yx

Try it online!

Explanation

_          We have to copy the input because
           MathGolf outputs 0 over an empty stack.
 {2‼%/}    Mod-div by 2 input times over input
       ]   Wrap whole stack into a list
        y  Join the list into a number
         x Reverse the number, removing leading zeroes
           Anyway, the old 05AB1E answer used it, so I guess it's valid...
|improve this answer|||||
\$\endgroup\$
3
\$\begingroup\$

Erlang (escript), 36 bytes

Port of various answers.

b(0)->0;b(N)->N rem 2+10*b(N div 2).

Try it online!

Erlang (escript), 37 bytes

Outputs a list of integers; it's longer by 1 byte anyway with concatenation ...

b(0)->[];b(N)->b(N div 2)++[N rem 2].

Try it online!

|improve this answer|||||
\$\endgroup\$
3
\$\begingroup\$

PHP, 55 bytes

for($p=2**32;$p=$p/2%$p;)echo($f+=$v=$argn/$p%2)?$v:'';

Try it online!

If concatenation is allowed, then see the other PHP answer by @Kaddath.

How

Take digits starting from most significant bit, which is leftmost. To strip leading zeroes, just accumulate the bit values until it goes above 0, then start displaying.

Doesn't work for zero! Not sure if I'm allowed to have the power (**) operator?

|improve this answer|||||
\$\endgroup\$
3
\$\begingroup\$

JavaScript (Node.js), 28 bytes

f=x=>x?x%2+10*f((x-x%2)/2):x

Try it online!

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

brainfuck, 50 bytes

>,[[->>>+<[-<+>>-]>[-<+>>]<<<<]+>>[-<<+>>]<]<[-.<]

Try it online! (with 10 as input)... Or try this naïve version with human-readable output, where I just added 48 + signs by hand, so that you guys can read the output.

How it works:

Reads the input and does the following, iteratively:

  • divide by 2, finding quotient and remainder
  • save the remainder + 1 on the left (this will make it easier to retrace the binary representation later)
  • move the tape right and repeat with this quotient as the new input

Then we walk left, printing the remainders, by first decreasing them by 1 to restore their real value.

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

Perl 6, 26 bytes

{[R~] ($_,*div 2...^0)X%2}

Try it online!

Uses concatenation, as well as some questionable interpretations of loops for [R~], ... and X.

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

Japt, 9 bytes

©u +A*ßUz

Try it

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

Jelly, 8 bytes

HḞпṖḂṚḞ

Try it online!

A monadic link taking an integer and returning a list of binary digits.

Explanation

 Ḟп     | While the current value converted to integer (by flooring) is non-zero, do the following, collecting up intermediate values:
H        | - Halve
    Ṗ    | Remove last
     Ḃ   | Mod 2
      Ṛ  | Reverse list
       Ḟ | Convert to integer
|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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