# Binary to decimal converter

As far as I can see, we don't have a simple binary to decimal conversion challenge.

Write a program or function that takes a positive binary integer and outputs its decimal value.

You are not allowed to use any builtin base conversion functions. Integer-to-decimal functions (e.g., a function that turns 101010 into [1, 0, 1, 0, 1, 0] or "101010") are exempt from this rule and thus allowed.

Rules:

• The code must support binary numbers up to the highest numeric value your language supports (by default)
• You may choose to have leading zeros in the binary representation
• The decimal output may not have leading zeros.
• Input and output formats are optional, but there can't be any separators between digits. (1,0,1,0,1,0,1,0) is not a valid input format, but both 10101010 and (["10101010"]) are.
• You must take the input in the "normal" direction. 1110 is 14 not 7.

Test cases:

1
1

10
2

101010
42

1101111111010101100101110111001110001000110100110011100000111
2016120520371234567


This challenge is related to a few other challenges, for instance this, this and this.

• Related Dec 5 '16 at 19:43
• Does the output have to be unsigned or can it be signed? Also, if my language happens to automatically switch between 32-bit and 64-bit integers depending on the length of the value, can the output be signed in both ranges? Eg- There's two binary values that will convert to decimal -1 (32 1's and 64 1's)
– milk
Dec 5 '16 at 20:47
• Also, can the output be floating, do does it need to be an integer? Dec 5 '16 at 21:17
• @Carcigenicate It must be an integer, but it can be of any data type. As long as round(x)==x you're fine :) 2.000 is accepted output for 10. Dec 5 '16 at 21:31
• Oh sweet. Thanks. Dec 5 '16 at 21:33

# SmileBASIC, 47 44 bytes

INPUT B$WHILE""<B$N=N*2+VAL(SHIFT(B$))WEND?N  Another program of the same size: INPUT B$WHILE""<B$N=N*2OR"0"<SHIFT(B$)WEND?N


# Keg, -rr, 14 12 bytes

0&÷^⑷2⑻Ë*⑹⑸⅀


Try it online!

## Explained

0&              # Place 0 into the register. This will serve as the power to which 2 will be raised
÷^            # Take the input as a number and split it into it's individual numbers. It is then reversed.
⑷           # Start a map applying the following to each item in the input:
2⑻Ë*      #   Exponate 2 to the power of the register and multiply the number by that value
⑹     #   Increment the register
⑸⅀  # Close the map and sum the stack, printing that sum implicitly


# Perl 5, 18 + 1 (-p) = 19 bytes

Combines techniques from the other Perl entries. Scored using the rules at the time of posting.

s/./$\+=$\+\$&/ge}{


Try it online!

# Japt-x, 6 bytes

Ô¬ËÑpE


Try it

# JavaScript (V8), 28 bytes

f=n=>~~n%2+(n<2?0:2*f(n/10))


Try it online!

Takes input as a Number, outputs a Number.

# Stax, 4 bytes

╖~♫p


Run and debug it

same reducing idea as Jelly.

# ARM Thumb-2 machine code, 16 bytes

2100 f810 2b01 b112 0852 4149 e7f9 4770


Commented assembly:

        .syntax unified
.arch armv6t2
.thumb
.globl bin2int
.thumb_func
// Input: null terminated string in r0
// Output: integer in r1
bin2int:
// Set initial result to zero
movs    r1, #0
.Lloop:
// Load char from r0 into r2, increment
ldrb    r2, [r0], #1
// Was it '\0'? If so, bail.
cbz     r2, .Lend
// Shift the lowest bit into the carry flag
// ASCII '0' has the lowest bit clear, ASCII '1'
// does not.
lsrs    r2, r2, #1
// Multiply the result by 2 (by adding to itself),
// and add the carry flag to that result
// Jump back to .Lloop (our loop condition is cbz)
b       .Lloop
.Lend:
// Return
bx      lr


The input is a null terminated string in r0 and the output is in r1.

I came up with the same idea as 640KB (SHR/ADC, or in my case, lsrs/adcs) without even seeing their solution. 😛

Great minds think alike, I guess.

# Husk, 5 bytes

Fo+Dd


Try it online!

F        # Fold from left
# (apply binary function to each element,
# together with the value of the previous result)
d    # over all the digits of the input,
# with this function:
o       # o = combination of 2 functions
+      # add second argument (each new digit, from left) to
D     # double the first argument (previous result)


# Turing Machine Code, 232 bytes

(Using, as usual, the morphett.info rule table syntax)

While only 40 bytes shorter than the existing solution, that solution uses 12 states whereas this one only uses 4:

0 1 0 l 0
0 0 . r 0
0 . 1 l 0
0 _ I r 1
0 I 2 r 0
0 2 3 r 0
0 3 4 r 0
0 4 5 r 0
0 5 6 r 0
0 6 7 r 0
0 7 8 r 0
0 8 9 r 0
0 9 X l 0
0 X O r 0
0 O I r 0
1 _ _ l 2
1 * * * 0
2 * _ l 3
3 O 0 l 3
3 I 1 l 3
3 . _ l 3
3 _ * * halt
3 * * l 3


### Try it online!

The interesting computation (ie. base-conversion) actually just takes place in state 0. This state decrements the binary number one-by-one, each time incrementing a decimal counter.

Due to naming clashes of the number-bases' alphabets, I make use of O and I during the conversion. State 1,2,3 only take care of cleaning the tape, converting the symbols O → 0 and I → 1 and finally halting the machine.