| Bytes | Lang | Time | Link |
|---|---|---|---|
| 170 | sed | 250818T135631Z | Toby Spe |
| 176 | Bespoke | 250818T073553Z | Josiah W |
| 029 | tinylisp 2 | 250814T204825Z | DLosc |
| 035 | TIBASIC TI84 Plus CE | 250814T145436Z | madeforl |
| 003 | Nekomata + n | 250804T060253Z | alephalp |
| 058 | Nim | 250803T122327Z | janAkali |
| 010 | Pip | 250803T051627Z | DLosc |
| 014 | Pip | 200908T015654Z | Razetime |
| 018 | APLNARS | 250729T193420Z | Rosario |
| 035 | AWK | 250729T133647Z | xrs |
| nan | Completely multiplication free | 240919T115514Z | RARE Kpo |
| 004 | Thunno 2 S | 230609T141647Z | The Thon |
| 3875 | Vyxal s | 230108T143436Z | The Thon |
| 048 | C | 230610T031701Z | Zoey |
| 084 | Commodore C64 BASIC | 230609T193917Z | Shaun Be |
| 051 | Desmos | 230329T025318Z | Aiden Ch |
| 010 | K ngn/k | 230205T064126Z | doug |
| 054 | Java 8 | 230206T091212Z | Fhuvi |
| 029 | TIBasic | 230205T162549Z | Youserna |
| 010 | shell with dc | 230205T024231Z | gildux |
| 048 | Excel | 230205T073303Z | Jos Wool |
| 031 | Arturo | 230205T070752Z | chunes |
| 070 | Go | 230205T050336Z | bigyihsu |
| 026 | JavaScript V8 | 200910T031134Z | Matthew |
| 033 | Raku | 230117T143750Z | Mark Ree |
| 031 | FunStack alpha | 230110T185159Z | DLosc |
| 041 | Elm | 230110T031908Z | naffetS |
| 232 | Turing Machine Code | 210315T145136Z | who care |
| 005 | Husk | 210130T222626Z | Dominic |
| 016 | ARM Thumb2 machine code | 210130T150923Z | EasyasPi |
| 004 | Stax | 210130T082712Z | Razetime |
| 010 | x8616 machine code | 200702T191808Z | 640KB |
| 037 | R | 200909T032151Z | DLosc |
| 006 | Japt x | 200908T151858Z | Shaggy |
| nan | Perl 5 | 200908T035412Z | Xcali |
| 055 | Excel | 200719T023432Z | General |
| 2565 | 7 | 200718T150412Z | Pizgenal |
| 012 | Keg | 200705T065517Z | lyxal |
| 052 | APLNARS | 200705T054957Z | user5898 |
| 044 | SmileBASIC | 170202T004143Z | 12Me21 |
| 037 | C | 161209T222511Z | user5634 |
| 047 | Forth gforth 0.7.3 | 161223T120126Z | 2xsaiko |
| 012 | Dyalog APL | 161206T230536Z | Adá |
| 012 | Retina | 161205T215807Z | Martin E |
| 049 | MATLAB | 161210T013217Z | hwm |
| 030 | Matlab | 161206T123619Z | Jonas |
| 036 | C# 6 | 161205T213854Z | Yytsi |
| 031 | Haskell | 161207T225335Z | Joshua D |
| 015 | Labyrinth | 161206T163649Z | Robert H |
| 064 | R 32bit | 161206T113454Z | djhurio |
| 044 | C | 161206T133826Z | Charles |
| 031 | JavaScript ES6 | 161206T005825Z | Neil |
| 036 | Javascript ES7 | 161205T204758Z | Shaun H |
| nan | Haskell | 161205T195206Z | nimi |
| nan | Perl | 161206T193514Z | Gabriel |
| 041 | Clojure | 161205T205314Z | Carcigen |
| 010 | Pushy | 161205T221147Z | FlipTack |
| 028 | ><> Fish | 161206T113758Z | Teal pel |
| 025 | Perl | 161205T211328Z | Dada |
| 012 | Befunge98 | 161206T095237Z | Leo |
| nan | Common Lisp | 161206T091802Z | Bart van |
| 019 | Minkolang v0.15 | 161206T074442Z | user4180 |
| 036 | ClojureScript | 161206T074237Z | MattPutn |
| 011 | Mathematica | 161205T235544Z | JungHwan |
| 040 | 아희Aheui | 161206T061729Z | JungHwan |
| 022 | Julia 0.5 | 161206T060954Z | Dennis |
| 037 | Ruby | 161206T041226Z | Depresse |
| 018 | Befunge | 161206T021307Z | James Ho |
| 032 | JavaScript ES6 | 161206T021137Z | ETHprodu |
| 030 | Python 2 | 161205T195253Z | Kade |
| 005 | Jelly | 161205T223333Z | lynn |
| 035 | JavaScript | 161205T230017Z | Titus |
| 007 | MATL | 161205T204911Z | DJMcMayh |
| 007 | MATL | 161205T223713Z | Luis Men |
| 083 | JavaScript | 161205T213920Z | Oliver |
| 044 | Mathematica | 161205T215752Z | Greg Mar |
| 053 | C | 161205T215437Z | edc65 |
| 272 | Turing Machine Code | 161205T215150Z | SuperJed |
| 038 | JavaScript ES6 | 161205T214316Z | edc65 |
| 010 | Jelly | 161205T212644Z | steenber |
| 044 | PHP | 161205T212904Z | Titus |
| 009 | Pyke | 161205T210733Z | Blue |
| 048 | Java | 161205T202213Z | Linnea G |
| 087 | Java 7 | 161205T202337Z | Poke |
| 047 | JavaScript ES7 | 161205T203611Z | darrylye |
| 028 | BrainFlak | 161205T200114Z | DJMcMayh |
| 008 | k | 161205T201920Z | skeevey |
| 055 | PowerShell v2+ | 161205T195251Z | AdmBorkB |
| 006 | 05AB1E | 161205T200110Z | Adnan |
| 029 | Bash + GNU utilities | 161205T200812Z | Digital |
| 064 | PHP | 161205T200250Z | Xanderha |
| 202 | TSQL | 161205T200042Z | Nelson |
| 007 | 05AB1E | 161205T194301Z | Emigna |
| 015 | Retina | 161205T194923Z | mbomb007 |
sed, 170 bytes
Any size integer can be converted, limited only by available memory.
y/01/oi/
s/^/0 /
:a
s/[5-9]\| i/@&/g
y/123456789/246802468/
:b
s/0@/1/
s/1@/2/
s/2@/3/
s/3@/4/
s/4@/5/
s/5@/6/
s/6@/7/
s/7@/8/
s/8@/9/
s/9@/@0/
s/^@/1/
tb
s/ ./ /
/ ./ba
How it works
#!/usr/bin/sed -f
# format: we have output on the left and input on the right,
# using o and i as binary digits 0 and 1
# # 23 ioi
# We multiply output by two and shift in the next bit of
# input:
# # 47 oi
# and repeat until all input is used up.
# set up - convert input to o/i and initialise result to 0
y/01/oi/
s/^/0 /
:a
# double result and add input's msb
# use @ to indicate carry from next place
s/[5-9]\| i/@&/g
y/123456789/246802468/
# propagate carry
:b
s/0@/1/
s/1@/2/
s/2@/3/
s/3@/4/
s/4@/5/
s/5@/6/
s/6@/7/
s/7@/8/
s/8@/9/
s/9@/@0/
s/^@/1/
tb
s/ ./ / # consume msb
/ ./ba # repeat until all input is consumed
If you want to watch it iterating, just stick a l command after the :a label:
0 iioiio$
1 ioiio$
3 oiio$
6 iio$
13 io$
27 o$
54
Bespoke, 176 bytes
base2I/O
a group of limited numbers:base2is in zeros together with included ones
and in base2integers,specific digit=power of2in this position
convert decimal out of a binary N
Assumes at least one digit is given.
tinylisp 2, 29 bytes
(\(B)(}(\(A D)(+(o D)A A))B 0
Explanation
(\(B)(}(\(A D)(+(o D)A A))B 0))
(\(B) ) ; Anonymous function, takes a string B
(} B 0) ; Left-fold B with a starting value of 0
(\(A D) ) ; on this function (A is accumulator, D is digit):
(o D) ; 1 if D is odd, 0 if even (convert char to 0/1)
(+ A A) ; Add that value to twice the accumulator
TI-BASIC (TI-84 Plus CE), 35 bytes
Prompt A
For(J,0,log(A
B+2^Jremainder(A,10→B
int(.1A→A
End
B
I know there's already an answer for this in TI-BASIC, i just thought it would be fun to do one my way. ^^
Nekomata + -n, 3 bytes
~Ɗƶ
This is extremely slow.
~Ɗƶ
Treat the input as a decimal number n
~ Find a number in range 0 to n-1
Ɗƶ such that all decimal digits of it are smaller than 2
-n counts the number of solutions.
For example, when the input is 10, ~ may give any of 0, 1, 2, ..., 9. Only 0 and 1 satisfy the condition that all decimal digits are smaller than 2, so there are 2 solutions.
Nim, 78 58 bytes
-20 bytes thanks to DLosc
proc g(s=""):int=(if$0>s:0
else:s[0..^2].g*2+s[^1].int-48)
Ungolfed:
proc parseNum(binary=""):int =
if $0 > binary: # `$` is toString operator; "0" > ""
0
else:
binary[0..^2].parseNum*2 + # UFCS, same as parseNum(binary[0..binary.len-2])
binary[^1].int - 48 # digit to int conversion
Pip, 10 bytes
FiaY2*y+iy
Explanation
Somewhat surprisingly, the shortest solution is the iterative one.
FiaY2*y+iy
a is command-line argument; y is "" (implicit)
Fia For each i in a:
2*y Two times the current value of y (treated as 0 when y is "")
+i Add i
Y Store that result back in y
y After the loop, output the final value of y
Alternate approaches
Map-sum (11 bytes):
RaMS_*E{Uv}
Array programming (12 bytes):
$+(^Ra)*E,#a
Recursive (12 bytes):
a&%a+2*RE@<a
Map-enumerate (12 bytes, a lightly golfed version of Razetime's answer):
$+:E_*BME Ra
Pip, 17 14 bytes
$+(**_*BMERVa)
Doesn't work on TIO because unary ** is only in the latest Pip version.
APL(NARS), 18 chars
+/k×2x*⌽¯1+⍳≢k←⍎¨⎕
This would convert the string of '0' and '1' with a array of 0 and 1, and from that it seems gets a result.
test:
+/k×2x*⌽¯1+⍳≢k←⍎¨⎕
⎕:
'0'
0
+/k×2x*⌽¯1+⍳≢k←⍎¨⎕
⎕:
'1'
1
+/k×2x*⌽¯1+⍳≢k←⍎¨⎕
⎕:
'101010'
42
+/k×2x*⌽¯1+⍳≢k←⍎¨⎕
⎕:
'11011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111'
4995366924470859583704000893073232977339613183
AWK, 35 bytes
{for(;j++<NF;)x+=$j*2^(NF-j)}1,$0=x
{for(;j++<NF;) # traverse chars
x+= # sum
$j* # catch 0 or 1
2^(NF-j)} # calc value
1,$0=x # set output
Completely multiplication free, division/modulo free, function call-free, pre-made reference string/table-free, and bit-shifting free awk approach that can prints out the cumulative value of MSB to current bit :
echo 10000101010001000010100000101010001101000110001110101 |
awk '{ for (__ = _ = _ < _; _++ < NF; ) $_ = __ += __ + $_ } _' FS=
1
2
4
8
16
33
66
133
266
533
1066
2132
4264
8529
17058
34116
68232
136464
272929
545858
1091717
2183434
4366868
8733736
17467472
34934944
69869889
139739778
279479557
558959114
1117918229
2235836458
4471672916
8943345832
17886691665
35773383331
71546766662
143093533325
286187066650
572374133300
1144748266600
2289496533201
4578993066403
9157986132806
18315972265612
36631944531224
73263889062449
146527778124899
293055556249799
586111112499598
1172222224999197
2344444449998394
4688888899996789
With just a tiny bit of code changes, one could do other base conversions to decimal too, like base-3 to decimal for the same number :
echo 211202212222101211101221020222221 |
awk '{ for (__ = _ = _ < _;_++ < NF; ) $_ = __ += __ + $_ + __ } _' FS=
2
7
22
68
204
614
1844
5533
16601
49805
149417
448253
1344760
4034280
12102841
36308525
108925576
326776729
980330188
2940990564
8822971693
26468915081
79406745245
238220235736
714660707208
2143982121626
6431946364878
19295839094636
57887517283910
173662551851732
520987655555198
1562962966665596
4688888899996789
Octal conversions require a tiny bit of nesting but nothing all that different
echo 205210240521506165 |
awk '{ for (__ = _ = _<_;_++ < NF;) $_ = __ += (__ += __ += __) + $_ } _' FS=
2
16
133
1066
8529
68232
545858
4366868
34934944
279479557
2235836458
17886691665
143093533325
1144748266600
9157986132806
73263889062449
586111112499598
4688888899996789
Thunno 2 S, 4 bytes
drVO
Explanation
drVO # Implicit input -> 101010
d # Cast to digits -> [1, 0, 1, 0, 1, 0]
r # Reverse -> [0, 1, 0, 1, 0, 1]
V # Truthy indices -> [1, 3, 5]
O # Two power -> [2, 8, 32]
# S flag sums -> 42
# Implicit output
Old:
dŻrO× # Implicit input -> 101010
d # Cast to digits -> [1, 0, 1, 0, 1, 0]
Ż # Length range -> [1, 0, 1, 0, 1, 0] [0, 1, 2, 3, 4, 5]
r # Reverse -> [1, 0, 1, 0, 1, 0] [5, 4, 3, 2, 1, 0]
O # Two power -> [1, 0, 1, 0, 1, 0] [32, 16, 8, 4, 2, 1]
× # Multiply -> [32, 0, 8, 0, 2, 0]
# S flag sums -> 42
# Implicit output
Vyxal s, 31 bitsv2, 9 7 3.875 bytes
fṘTE
fṘTE
- -2 thanks to Steffan
- -3 chars thanks to lyxal
Explanation:
fṘTE # Implicit input
f # Flatten into a list of digits
Ṙ # Reverse the list
T # Truthy indices
E # Two power
# Implicit output of sum
Old:
ṘėRvƒ↲∑ # Implicit input
Ṙė # Reverse and enumerate
R # Reverse each
vƒ # Vecorised reduce over:
↲ # Left bit-shift
∑ # Sum
# Implicit output
C, 48 bytes
i;f(char *s){for(i=0;*++s;i+=i+*s-48);return i;}
Heavily inspired by @edc45 's answer but I made some significant changes. I made 'i' a global variable to save having to typecast. I also used a for loop to save a few bytes. It does now require a single leading 0 in the binary representation.
More readable version
f(char *s){
int i = 0; // Total decimal counter
for(;;*++s;i+=i+*s-48); // Shift left and add 0 or 1 depending on charecter
return i;
}
Commodore C64 BASIC, 84 BASIC Bytes
0inputa$:b=len(a$):ifb=.thenend
1t=.:c=t:forx=bto1step-1:c=c+1:t=t+val(mid$(a$,c,1))*2^(x-1):next:?t
How it works
Firstly, the inputa$ is expecting the user to enter a string, though the only sanity check is that there is something is entered. We then initialise three variables, b to the string length, t to zero (in the Commodore BASIC interpreter, parsing . is slightly quicker than parsing 0), and c also to zero. As we enter the loop, we read each position from right to left.
For each position, the value in c is incremented by 1, and t adds the value of each position multiplied by 2 the power of the current bit position minus 1, and it continues until each position is calculated. The result is then printed.
Desmos, 51 bytes
f(n)=∑_{i=0}^{log(n+0^n)}2^imod(floor(n/10^i),10)
Try It On Desmos! - Prettified
Without the ban on binary list input, this could be 33 bytes:
f(l)=total(2^{[l.length-1...0]}l)
Java 8, 54 bytes
Solution using Streams only :
s->s.chars().mapToLong(e->e).reduce(0,(a,b)->2*a+b-48)
If the question didn't require a long, the answer could have been shorter and more elegant (38 bytes) :
s->s.chars().reduce(0,(a,b)->2*a+b-48)
TI-Basic, 29 bytes
sum(seq(sub(Ans,length(Ans)-I,1)2^I,I,0,length(Ans)-1
Takes input as a string in Ans.
shell with dc, 16 13 10 bytes
There's already a bash + GNU utils, but this solution is POSIX compliant and shorter.
dc -e "10o2i$1p"
It's also a bit quicker (we use internal functioning instead of extra computation, plus there's only one process call instead of two.) But, quoting and extra spaces aren't mandatory
dc -e10o2i$1p
Now, explanation of the expression (-e argument)
10 oask to set Output base to decimal.2 iask to set Input base to binary.
Note that the order matters here. If we issue first2i,10owould be read as request to set to binary…$1 pask to Print out the entry…
Read then as binary and expressed in decimal form.
Better, on standard installations with no DC_ENV_ARGS or others (depending your implementation) variable set, default base is decimal. So, finally
dc -e2i$1p
Note : dc is a big bases-converter where i tells how to parse inputs (kinda scanf()) and o tells how to display it (kinda printf()). Values are always stored and manipulated using DCB representation.
Excel, 51 48 bytes
=NPV(-0.5,MID(n,SORT(SEQUENCE(LEN(n)),,-1),1)/2)
Input n
Go, 70 bytes
func(s string)(o uint64){for _,r:=range s{o=2*o+uint64(r-'0')}
return}
Takes in a string of binary digits (01). For each digit, convert it to it's corresponding number, and add it to twice the total.
JavaScript (V8), 28 26 bytes
-2 bytes thank to l4m2!
f=n=>n%2|(n<2?0:2*f(n/10))
Takes input as a Number, outputs a Number.
Raku, 33 bytes
{.flip.comb.kv.map(2*** * *).sum}
(If builtins were allowed, this would just be .parse-base(2).)
Explanation:
.flip reverses the string, .comb converts it into a list of chars, .kv inserts the index of each char in front of it. The map block consumes two elements at a time, computing the product of 2 to the power of the first argument (the index) all multiplied by the second argument (the 0 or 1), and the resulting list of products is totaled by .sum.
The asterisks in the map call are doing triple duty: the first two are the exponentiation operator, while the third is a Whatever standing for the first argument to the block (and is the reason the expression becomes a block without curlies). Likewise, the fourth is the standard ASCII multiplication operator, while the fifth is another Whatever representing the second argument. So this ungolfed version does the same thing:
… .map( { 2**$^k × $^v } ) …
FunStack alpha, 31 bytes
Plus Double compose foldl1 Odd?
Takes a string consisting of 0s and 1s, outputs an integer. Try it at Replit: pass the input string as a command-line argument (formatted like \"1101\") and enter the program on stdin.
Explanation
Same idea as Steffan's Elm answer, Dominic van Essen's Husk answer, etc.
Plus Double compose
Push the functions Plus (dyadic, adds its arguments) and Double (monadic, doubles its argument. Compose them, resulting in a dyadic function that doubles its first argument and adds the result to its second argument.
foldl1
Perform a left fold over a list using that function, with the first element of the list serving as the initial accumulator value. This errors if the list is empty, but we don't have to worry about that since the input represents a positive integer.
Odd?
The list that we want to fold over is the program argument (a string, aka list of characters) with the character '0' turned into the number 0 and similarly for '1' and 1. The obvious way to do this is Minus '0', but since we only have to deal with 0 and 1, we can use Odd? instead. This function returns 1 if its argument is odd and 0 if it is even; when applied to characters, it considers whether the charcode is odd or even.
Another 31-byte answer is possible using a different approach:
Sum Times Pow 2 #N Reverse Odd?
This generates an infinite list of powers of 2, multiplies it by the input digits reversed, and sums the result.
Elm, 60 41 bytes
String.foldl(\b a->2*a+Char.toCode b-48)0
-19 bytes thanks to Wheat Wizard and alephalpha
You can try it here. Here's a full test snippet:
import Html exposing (text)
f : String -> Int
f=String.foldl(\b a->2*a+Char.toCode b-48)0
main = text (String.fromInt (f "101010"))
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.
Husk, 5 bytes
Fo+Dd
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)
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
adcs r1, r1
// 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.
x86-16 machine code, 10 bytes
Binary:
00000000: 31d2 acd0 e811 d2e2 f9c3 1.........
Listing:
31 D2 XOR DX, DX ; clear output value
CHRLOOP:
AC LODSB ; load next char into AL
D0 E8 SHR AL, 1 ; CF = LSb of ASCII char
11 D2 ADC DX, DX ; shift CF left into result
E2 F9 LOOP CHRLOOP ; loop until end of string
C3 RET ; return to caller
Callable function, input string in [SI] length in CX. Result in DX.
Example test program I/O:
R, 48 37 bytes
-11 bytes thanks to Dominic van Essen
sum(utf8ToInt(scan(,""))%%2*2^(31:0))
Takes input as a string, left-padded with 0s to 32 bits. Try it online!
I copied some good ideas from djhurio's much earlier R answer, so go give that an upvote too. As with the first solution there, this solution won't work for the last test case because it's too large to fit into R's default size of integer.
Explanation
To get a vector of the bits as integers 0 and 1, we use uft8ToInt to convert to a vector of character codes. This gives us a list of 48s and 49s, which we take mod 2 to get 0s and 1s.
Then, to get the appropriate powers of 2 in descending order, we construct a range from 31 down to 0. Then we convert each of those numbers to the corresponding power of two (2^).
Finally, we multiply the two vectors together and return their sum.
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}{
Excel, 74 70 55
Trailing parens already discounted. Tested in Excel Online.
Formulae:
A1: InputB1:=LEN(A1)(7)
Main Code (48):
A pretty simple "add all the powers of 2" formula:
=SUM(MID(A1,1+B1-SEQUENCE(B1),1)/2*2^SEQUENCE(B1))
Verify with:
=DECIMAL(A1,2)
7, 25 bytes (65 characters)
07717134200446170134271600446170001755630036117700136161177546635
Explanation
077 Initialize counter to 0 (an empty section) 17134200..77546635 Push the main loop onto the frame
Once the original code has finished executing, the frame is
||6||7134200447013427160044700017556300377700136177754635
The last section of this is the main loop, which will run repeatedly until it gets deleted. All of it except the last two commands is data and section separators, which push commands onto the frame to leave it as:
||6|| (main loop) |73426644|6734216644|6667553663||00137||54
3 outputs the last section (54) and discards the last two bars. On the first iteration, 5 is interpreted as a switch to output format 5 ("US-TTY"), which converts each pair of commands to a character. The 4 following it does not form a group, so it is ignored. On future iterations, we are already in output format 5, so 54 is interpreted as a group meaning "input character". To do this, the character from STDIN is converted into its character code (or, on EOF, -1), and 1 is added. Then, the last section (which is now the 00137) is repeated that many times. In summary:
- If the character is
0,00137is repeated 49 times. - If the character is
1,00137is repeated 50 times. - If there are no more characters,
00137is repeated 0 times (i.e. deleted). - If this was the first iteration,
00137is left alone (i.e. repeated 1 time).
5 removes the last section (00137 repeated some number of times) from the frame and executes it, resulting in this many sections containing 6673.
The main loop has now finished executing, so the last section (which is 6673 unless we reached EOF) runs. 66 concatenates the last three sections into one (and has some other effects that don't affect the number of sections), which 73 deletes. Thus, the last three sections are deleted, and this repeats until the last section is no longer 6673.
- If we reached EOF, nothing happens.
- If this is the first iteration or the character was
0, after removing the last three sections 0 or 16 times, there is just one copy left on the frame. This copy deletes itself and the two sections before it, leaving everything up to the main loop and the section after it. - If the character was
1, after removing the last three sections 17 times, all 50 copies and the third section after the main loop (the EOF handler) have been deleted.
Now, the last section left on the frame runs. This could, depending on the inputted character, be any of the three sections after the main loop.
If this is the first iteration or the character was 0, the section just after the main loop, 73426644, runs.
73 deletes this section, and 4 swaps the main loop with the counter behind it. This counter keeps track of the number we want to output, stored as the number of 7s and 1s minus the number of 6s and 0s. This metric has the property that it is not changed by pacification (which changes some 6s into 0s, some 7s into 1s, and sometimes inserts 7...6 around code). 2 duplicates the counter and 6 concatenates the two copies (after pacifying the second, which, as we saw, does not change the value it represents), so the value is doubled. If this was the first iteration, the counter was previously empty, and doubling it still results in an empty section. Then, 6 gets rid of the empty section inserted by 4 (and pacifies the counter again, leaving the value unchanged), and 44 swaps the counter back to its original position. This leaves two empty sections on the end of the frame, which are removed automatically, and the main loop runs again.
If the character was 1, the second section after the main loop (6734216644) runs. This is just like the section before it (explained in the last paragraph), except for two extra commands: a 6 at the start, which joins the section with the previous one before 73 deletes it, and a 1 in the middle, which adds a 7 to the counter (increasing its value by 1) after it gets doubled.
If we reached EOF, the third section after the main loop (6667553663) runs. 666 joins the last four sections (everything after the counter) into one section, to be deleted by 3. 7553 exits output format 5 by outputting 55 and deletes the section before it, leaving only ||6| (counter) on the frame. 66 pacifies the two sections and combines them, turning the 6 back into a 0 (and not changing the value of the counter). Finally, the last 3 outputs everything.
The 0 at the start enters output format 0 ("Numerical output"), and the rest of the section (i.e. the counter) is converted to a number by taking the number of 7s and 1s minus the number of 6s and 0s. This value, which is the input converted from binary, is output in decimal and the program terminates.
Keg, -rr, 14 12 bytes
0&÷^⑷2⑻Ë*⑹⑸⅀
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
APL(NARS), 26 chars, 52 bytes
{+/a×⌽1,2x*⍳¯1+≢a←1-⍨⎕D⍳⍵}
test:
f←{+/a×⌽1,2x*⍳¯1+≢a←1-⍨⎕D⍳⍵}
⎕fmt f"0"
0
~
f"1"
1
⎕fmt f"101010"
42
~~
f"10"
2
(≢,f)"11011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
152 4995366924470859583704000893073232977339613183
152 bits... It should be limited from memory for store string and numbers.
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
Forth (gforth 0.7.3), 47 bytes
: x 2 base ! bl parse s>number drop decimal . ;
: x - define new word with name 'x'
2 base ! - set base to binary
bl parse - read line until a space (bl) or EOL
s>number - try to convert the string to number
drop - we only want the converted number and not the success flag
decimal - set base to decimal
. - print value on top of stack
; - end of definition
Test cases:
x 1 1 ok
x 10 2 ok
x 101010 42 ok
x 1101111111010101100101110111001110001000110100110011100000111 2016120520371234567 ok
Dyalog APL, 12 bytes
(++⊢)/⌽⍎¨⍞
⍞ get string input
⍎¨ convert each character to number
⌽ reverse
(...)/ insert the following function between the numbers
++⊢ the sum of the arguments plus the right argument
ngn shaved 2 bytes.
Retina, 12 bytes
Byte count assumes ISO 8859-1 encoding.
+%`\B
¶$`:
1
Alternative solution:
+1`\B
:$`:
1
Explanation
This will probably be easier to explain based on my old, less golfed, version and then showing how I shortened it. I used to convert binary to decimal like this:
^
,
+`,(.)
$`$1,
1
The only sensible way to construct a decimal number in Retina is by counting things (because Retina has a couple of features that let it print a decimal number representing an amount). So really the only possible approach is to convert the binary to unary, and then to count the number of unary digits. The last line does the counting, so the first four convert binary to unary.
How do we do that? In general, to convert from a list of bits to an integer, we initialise the result to 0 and then go through the bits from most to least significant, double the value we already have and add the current bit. E.g. if the binary number is 1011, we'd really compute:
(((0 * 2 + 1) * 2 + 0) * 2 + 1) * 2 + 1 = 11
^ ^ ^ ^
Where I've marked the individual bits for clarity.
The trick to doing this in unary is a) that doubling simply means repeating the number and b) since we're counting the 1s at the end, we don't even need to distinguish between 0s and 1s in the process. This will become clearer in a second.
What the program does is that it first adds a comma to the beginning as marker for how much of the input we've already processed:
^
,
Left of the marker, we'll have the value we're accumulating (which is correctly initialised to the unary representation of zero), and right of the value will be the next bit to process. Now we apply the following substitution in a loop:
,(.)
$`$1,
Just looking at ,(.) and $1,, this moves the marker one bit to the right each time. But we also insert $`, which is everything in front of the marker, i.e. the current value, which we're doubling. Here are the individual steps when processing input 1011, where I've marked the result of inserting $` above each line (it's empty for the first step):
,1011
1,011
_
110,11
___
1101101,1
_______
110110111011011,
You'll see that we've retained and doubled the zero along with everything else, but since we're disregarding them at the end, it doesn't matter how often we've doubled them, as long as the number of 1s is correct. If you count them, there are 11 of them, just what we need.
So that leaves the question of how to golf this down to 12 bytes. The most expensive part of the 18-byte version is having to use the marker. The goal is to get rid of that. We really want to double the prefix of every bit, so a first idea might be this:
.
$`$&
The problem is that these substitutions happen simultaneously, so first bit doesn't get doubled for each bit, but it just gets copied once each time. For input 1011 we'd get (marking the inserted $`):
_ __ ___
1101011011
We do still need to process the input recursively so that the doubled first prefix is doubled again by the second and so on. One idea is to insert markers everywhere and repeatedly replace them with the prefix:
\B
,
+%`,
¶$`
After replacing each marker with the prefix for the first time, we need to remember where the beginning of the input was, so we insert linefeeds as well and use the % option to make sure that the next $` only picks up things up the closest linefeed.
This does work, but it's still too long (16 bytes when counting 1s at the end). How about we turn things around? The places where we want to insert markers are identified by \B (a position between two digits). Why don't we simply insert prefixes into those positions? This almost works, but the difference is that in the previous solution, we actually removed one marker in each substitution, and that's important to make the process terminate. However, the \B aren't character but just positions, so nothing gets removed. We can however stop the \B from matching by instead inserting a non-digit character into this place. That turns the non-word boundary into a word boundary, which is the equivalent of removing the marker character earlier. And that's what the 12-byte solution does:
+%`\B
¶$`:
Just for completeness, here are the individual steps of processing 1011, with an empty line after each step:
1
1:0
10:1
101:1
1
1:0
1
1:0:1
1
1:0
10:1:1
1
1:0
1
1:0:1
1
1:0
1
1:0:1:1
Again, you'll find that the last result contains exactly 11 1s.
As an exercise for the reader, can you see how this generalises quite easily to other bases (for a few additional bytes per increment in the base)?
MATLAB, 49 bytes
@(a)dot(int2str(a)-'0',2.^(floor(log10(a)):-1:0))
Anonymous function that splits the input into an array with int2str(a)-'0', then does a dot product with powers of 2. Has rounding error for the last test case, will update the solution when I figure out a fix.
Matlab, 30 Bytes
@(x)sum(2.^find(flip(x)-48)/2)
The last test case has rounding errors (because of double), so if you need full precision:
@(x)sum(2.^uint64(find(flip(x)-48))/2,'native')
with 47 Bytes.
C# 6, 85 37 36 bytes
long b(long n)=>n>0?n%2+2*b(n/10):0;
- Thanks to Kade for saving 41 bytes!
- Changing to C# 6 saved another 7 bytes.
Haskell, 31 bytes
f=foldl(\a b->2*a+(read$b:[]))0
Takes input in string format (e.g. "1111"). Produces output in integer format (e.g. 15).
:[] Converts from an element to an array -- in this chase from Char to [Char] (String).
read Converts from string to whatever context it's in (in this case the context is addition, so converts to Num)
so (read$b:[]) converts b from Char to Num.
a is the accumulator, so multiply that by two and add the Num version of b.
If input in the format [1,1,1,1] was allowed, the 18 byte
f=foldl((+).(2*))0
would work, but since it's not, it doesn't.
Labyrinth, 17 15 bytes
-+:
8 +
4_,`)/!
Labyrinth is a two-dimensional, stack-based language. In labyrinth, code execution follows the path of the code like a maze with spaces acting as walls and beginning at the top-left-most non-space character. The code flow is determined by the sign of the top of the stack. Since the stack has implicit zeroes at the bottom, the first four instructions (-+:+) have no effect.
Loop starting at the ,
,Push the ascii code value of the next input character to the stop of the stack, or push -1 if EOF._48pushes 48 to the top of the stack-Pop y, pop x, pushx-y. The previous instructions have the effect of subtracting 48 from the input yielding 0 for "0" and 1 for "1".+Pop y, pop x, pushx+y.:Duplicate the top of the stack+This and the previous instruction have the effect of multiplying the current value by 2
So the circular part of the code, in effect, multiples the current number by 2 and adds either a 1 or a 0 depending on if the character 1 or 0 was input.
Tail
If the top of the stack is negative (meaning EOF was found), the code will turn left at the junction (going towards the semicolon).
- ``` Negate the top of the stack to get 1
)Icrement the top of the stack to get 2/Pop y, pop x, push x/y (integer division). This has the effect of undoing the last*2from the loop.!Output the integer representation of the top of the stack. At this point the program turns around because it hit a dead end and then exits with an error because it tries to divide by zero.
Thanks to @Martin Ender for saving me 2 bytes (and teaching me how to better think in Labyrinth).
R (32-bit), 64 Bytes
Input for the function should be given as character. The base functions of R support 32-bit integers.
Input:
# 32-bit version (base)
f=function(x)sum(as.double(el(strsplit(x,"")))*2^(nchar(x):1-1))
f("1")
f("10")
f("101010")
f("1101111111010101100101110111001110001000110100110011100000111")
Output:
> f("1")
[1] 1
> f("10")
[1] 2
> f("101010")
[1] 42
> f("1101111111010101100101110111001110001000110100110011100000111")
[1] 2.016121e+18
R (64-bit), 74 Bytes
Input for the function should be given as character. The package bit64 has to be used for 64-bit integers.
Input:
# 64-bit version (bit64)
g=function(x)sum(bit64::as.integer64(el(strsplit(x,"")))*2^(nchar(x):1-1))
g("1")
g("10")
g("101010")
g("1101111111010101100101110111001110001000110100110011100000111")
Output:
> g("1")
integer64
[1] 1
> g("10")
integer64
[1] 2
> g("101010")
integer64
[1] 42
> g("1101111111010101100101110111001110001000110100110011100000111")
integer64
[1] 2016120520371234567
C, 44 bytes
d(s,v)char*s;{return*s?d(s,v+=v+*s++-48):v;}
Use as follows :
int main(){
printf("%i\n", d("101010",0));
}
Remove two bytes and an unused parameter thanks to Steadybox
JavaScript (ES6), 33 31 bytes
s=>[...s].map(c=>r+=+c+r,r=0)|r
Edit: Shorter but less sweet: 2 bytes saved thanks to @ETHproductions.
Javascript (ES7) 41 40 36 bytes
f=([c,...b])=>c?c*2**b.length+f(b):0
takes a string as input
Shaved a byte thanks to ETHproductions
f=([c,...b])=>c?c*2**b.length+f(b):0
document.write([
f('101010'),
f('11010'),
f('10111111110'),
f('1011110010110'),
].join("<br>"))
Haskell, 16 111 + 57 = 168 bytes
import Data.String
instance IsString[Int]where fromString=map((-48+).fromEnum)
f::[Int]->Int
f=foldl1((+).(2*))
+57 bytes for the compile flags -XOverloadedStrings, -XOverlappingInstances and -XFlexibleInstances.
The challenge has some cumbersome IO format, because it heavily depends on how data types are expressed in the source code. My first version (16 bytes), namely
foldl1((+).(2*))
takes a list of integers, e.g. [1,0,1,0,1,0] and was declared invalid because literal Haskell lists happen to have , between the elements. Lists per se are not forbidden. In my new version I use the very same function, now named f, but I overload "Quote enclosed character sequences". The function still takes a list of integers as you can see in the type annotation [Int] -> Int, but lists with single digit integers can now be written like "1234", e.g.
f "101010"
which evaluates to 42. Unlucky Haskell, because the native list format doesn't fit the challenge rules. Btw, f [1,0,1,0,1,0] still works.
Perl, 21 19 16 + 4 = 20 bytes
-4 bytes thanks to @Dada
Run with -F -p (including the extra space after the F). Pipe values to the function using echo -n
$\+=$_+$\for@F}{
Run as echo -n "101010" | perl -F -pE '$\+=$_+$\for@F}{'
I feel this is sufficiently different from @Dada's answer that it merits its own entry.
Explanation:
-F #Splits the input character by character into the @F array
-p #Wraps the entire program in while(<>){ ... print} turning it into
while(<>){$\+=$_+$\for@F}{print}
for@F #Loops through the @F array in order ($_ as alias), and...
$\+=$_+$\ #...doubles $\, and then adds $_ to it (0 or 1)...
while(<>){ } #...as long as there is input.
{print}#Prints the contents of $_ (empty outside of its scope), followed by the output record separator $\
This uses my personal algorithm of choice for binary-to-decimal conversion. Given a binary number, start your accumulator at 0, and go through its bits one by one. Double the accumulator each bit, then add the bit itself to your accumulator, and you end up with the decimal value. It works because each bit ends up being doubled the appropriate number of times for its position based on how many more bits are left in the original binary number.
Clojure, 114 105 63 41 bytes
V4: 41 bytes
-22 bytes thanks to @cliffroot. Since digit is a character, it can be converted to it's code via int, then have 48 subtracted from it to get the actual number. The map was also factored out. I don't know why it seemed necessary.
#(reduce(fn[a d](+(* a 2)(-(int d)48)))%)
V3: 63 bytes
(fn[s](reduce #(+(* %1 2)%2)(map #(Integer/parseInt(str %))s)))
-42 bytes (!) by peeking at other answers. My "zipping" was evidently very naïve. Instead of raising 2 to the current place's power, then multiplying it by the current digit and adding the result to the accumulator, it just multiplies the accumulator by 2, adds on the current digit, then adds it to the accumulator. Also converted the reducing function to a macro to shave off a bit.
Thanks to @nimi, and @Adnan!
Ungolfed:
(defn to-dec [binary-str]
(reduce (fn [acc digit]
(+ (* acc 2) digit))
(map #(Integer/parseInt (str %)) binary-str)))
V2: 105 bytes
#(reduce(fn[a[p d]](+ a(*(Integer/parseInt(str d))(long(Math/pow 2 p)))))0(map vector(range)(reverse %)))
-9 bytes by reversing the string so I don't need to create an awkward descending range.
V1: 114 bytes
Well, I'm certainly not winning! In my defense, this is the first program I've ever written that converts between bases, so I had to learn how to do it. It also doesn't help that Math/pow returns a double that requires converting from, and Integer/parseInt doesn't accept a character, so the digit needs to be wrapped prior to passing.
#(reduce(fn[a[p d]](+ a(*(Integer/parseInt(str d))(long(Math/pow 2 p)))))0(map vector(range(dec(count %))-1 -1)%))
Zips the string with a descending index representing the place number. Reduces over the resulting list.
Ungolfed:
(defn to-dec [binary-str]
(reduce (fn [acc [place digit]]
(let [parsed-digit (Integer/parseInt (str digit))
place-value (long (Math/pow 2 place))]
(+ acc (* parsed-digit place-value))))
0
(map vector (range (dec (count binary-str)) -1 -1) binary-str)))
Pushy, 10 bytes
Takes input as a list of 0/1 on the command line: $ pushy binary.pshy 1,0,1,0,1,0.
L:vK2*;OS#
The algorithm really shows the beauty of having a second stack:
\ Implicit: Input on stack
L: ; \ len(input) times do:
v \ Push last number to auxiliary stack
K2* \ Double all items
OS# \ Output sum of auxiliary stack
This method works because the stack will be doubled stack length - n times before reaching number n, which is then dumped into the second stack for later. Here's what the process looks like for input 101010:
1: [1,0,1,0,1,0] 2: [] 1: [2,0,2,0,2] 2: [0] 1: [4,0,4,0] 2: [2] 1: [8,0,8] 2: [2,0] 1: [16,0] 2: [2,0,8] 1: [32] 2: [2,0,8,0] 1: [] 2: [2,0,8,0,32] 2 + 8 + 32 -> 42
><> (Fish) 36 28 bytes
/i:1+?!v$2*$2%+!| !
/0| ;n~<
Edit 1: Forgot to put the output in the original. Added output and used MOD 2 instead of minus 48 to convert ascii to decimal to save the extra bytes lost. (no change in bytes)
Edit 2: Changed the algorithm completely. Each loop now does this; times current value by 2, then add the mod of the input. (saving of 8 bytes)
Try it Online! - This works with bigger numbers than the above link.
Perl, 25 bytes
-3 bytes thanks to @Dom Hastings.
24 bytes of code + 1 byte for -p flag.
$\|=$&<<$v++while s/.$//
To run it:
perl -pe '$\|=$&<<$v++while s/.$//' <<< 101010
Explanations:
$\|=$&<<$v++ # Note that: we use $\ to store the result
# at first $v=0, and each time it's incremented by one
# $& contains the current bit (matched with the regex, see bellow)
# So this operation sets a $v-th bit of $\ to the value of the $v-th bit of the input
while # keep doing this while...
s/.$// # ... there is a character at the end of the string, which we remove.
# $\ is implicitly printed thanks to -p flag
Befunge-98, 12 bytes
2j@.~2%\2*+
Reads one char at a time from input, converts it to 0 or 1 by taking its value modulo 2 (0 is char(48), 1 is char(49)), then uses the usual algorithm of doubling the current value and adding the new digit each time.
Bonus: This works with any kind of input string, I've been trying for a while now to find any funny input->output combination, but I wasn't able to produce anything (sadly, "answer"=46). Can you?
Common Lisp, 99 88 72 bytes
Takes a string as input
(defun f(s)(reduce(lambda(a d)(+ d(* a 2)))(map'list #'digit-char-p s)))
Ungolfed:
(defun bin-to-dec (bin-str)
(reduce (lambda (acc digit) (+ digit (* acc 2)))
(map 'list #'digit-char-p bin-str)))
Minkolang v0.15, 23 19 bytes
n6ZrI[2%2i;*1R]$+N.
Explanation
n gets input in the form of a number
6Z converts to string (so that it is split into an array)
r reverses it
I gets the stack length
[ ] for loop with the stack's length as the number of iterations
2% gets the modulo of the ascii value
1 =(string conversion)> 49 =(after modulo)> 1
0 =(string conversion)> 48 =(after modulo)> 0
2i; raises 2 to the power of the loop counter
* multiplies it by the modulo
1R rotates stack 1 time
$+ sums everything
N. outputs as number and exit
ClojureScript, 36 bytes
(fn[x](reduce #(+(* 2 %)(int %2))x))
or
#(reduce(fn[a n](+(* 2 a)(int n)))%)
The straightforward reduction. Takes a string as input.
Mathematica, 27 13 11 bytes
Fold[#+##&]
Accepts a List of bits as input (e.g. {1, 0, 1, 1, 0} -- Mathematica's binary representation of the number 22)
아희(Aheui), 40 bytes
아빟뱐썩러숙
뎌반뗘희멍파퍄
Accepts a string composed of 1s and 0s.
To try online
Since the online Aheui interpreter does not allow arbitrary-length strings as inputs, this alternative code must be used (identical code with slight modifications):
Add the character 벟 at the end of the first line (after 우) length(n)-times.
어우
우어
뱐썩러숙
번댜펴퍼망희땨
If the input is 10110, the first line would be 어우벟벟벟벟벟.
When prompted for an input, do NOT type quotation marks. (i.e. type 10110, not "10110")
Ruby, 37 bytes
ruby -e 'o=0;gets.each_byte{|i|o+=o+i%2};p o/2'
1234567890123456789012345678901234567
This depends on the terminating \n (ASCII decimal 10) being zero modulo 2 (and on ASCII 0 and 1 being 0 and 1 mod two, respectively, which thankfully they are).
Befunge, 20 18 bytes
Input must be terminated with EOF rather than EOL (this lets us save a couple of bytes)
>+~:0`v
^*2\%2_$.@
Explanation
> The stack is initially empty, the equivalent of all zeros.
+ So the first pass add just leaves zero as the current total.
~ Read a character from stdin to the top of the stack.
:0` Test if greater than 0 (i.e. not EOF)
_ If true (i.e > 0) go left.
%2 Modulo 2 is a shortcut for converting the character to a numeric value.
\ Swap to bring the current total to the top of the stack.
*2 Multiply the total by 2.
^ Return to the beginning of the loop,
+ This time around add the new digit to the total.
...on EOF we go right...
$ Drop the EOF character from the stack.
. Output the calculated total.
@ Exit.
JavaScript (ES6), 32 bytes
f=([...n])=>n+n&&+n.pop()+2*f(n)
Recursion saves the day again! Though the parameterization seems a little long...
Python 2, 49 37 31 30 Bytes
Now this will take a binary number in a decimal representation, since Python can handle arbitrarily large integers.
b=lambda n:n and n%2+2*b(n/10)
thanks to xnor for saving a byte :)
The easiest way to see how this works is by seeing a basic formula for converting binary to decimal:
= 101010
= 1*(2^5) + 0*(2^4) + 1*(2^3) + 0*(2^2) + 1*(2^1) + 0*(2^0)
= 1*32 + 0*16 + 1*8 + 0*4 + 1*2 + 0*1
= 42
This is a 'standard' way of converting. You can expand the third line like so:
= ((((1*2 + 0)*2 + 1)*2 + 0)*2 + 1)*2 + 0
And this is essentially what the recursive method I've made is doing.
Alternate solutions I had:
b=lambda n:n and n%10+2*b(n/10)
b=lambda n:n%10+2*(n and b(n/10))
b=lambda n:0if n<1else n%10+2*b(n/10)
b=lambda n:0**(n/10)or n%10+2*b(n/10)
b=lambda n,o=0:o*(n<'0')or b(n[1:],2*o+int(n[0]))
lambda j:sum(int(b)*2**a for a,b in enumerate(j,1))
Jelly, 5 bytes
DḤ+¥/
Explanation
The cast
Dis a monad (single argument function): digits, turning1234into[1, 2, 3, 4].Ḥis a monad that doubles its single argument.+is a dyad (two argument function) that adds its left and right arguments.
From there, it gets a little tricky.
Here’s what happens at parse time
D,Ḥ, and+are read. The chain looks like[D, Ḥ, +].The next two characters are quicks, which act like parse-time postfix operators on the links (functions) we've read so far.
When
¥is read, the last two links get popped and replaced by a link that acts like the dyad formed by composing them. So now the chain looks like[D, dyad(Ḥ+)].When
/is read, the last link (which ought to be a dyad) gets popped and replaced by a monad that folds using this dyad (intuitively:f/takes a list, replaces the commas in it withf, and evaluates the result.)The final chain looks like
[D, fold(dyad(Ḥ+))], two monads.
Here's what happens at run time
Input (a number) is implicitly read into the working value (say,
101010).Dis executed, replacing the working value with its digits ([1,0,1,0,1,0]).fold(dyad(Ḥ+))is executed, replacing the working value with1∗0∗1∗0∗1∗0, where∗is the dyadḤ+.
So what does x∗y evaluate to?
In a dyadic definition, the working value is initially the left argument,
x.Ḥ, the double monad, doubles this value. The working value is now2x.+, the plus dyad, lacks a right argument, so this is a hook: a special syntactical pattern where the right argument of this dyad gets injected into+. This yields2x + yas the final working value, which is returned.
So the whole expression evaluates to:
1∗0∗1∗0∗1∗0 = 2×(2×(2×(2×(2×1+0)+1)+0)+1)+0
= 32×1 + 16×0 + 8×1 + 4×0 + 2×1 + 1×0
= 42
JavaScript, 35 bytes
f=([c,...b],n=0)=>c<2?f(b,+c+n+n):n
For c='1' and c='0', c<2 returns true.
If b is empty, c will be undefined in the next recursion and c<2 will be false.
MATL, 7 bytes
PoofqWs
P % Implicitly input string. Reverse
o % Convert to array of ASCII codes
o % Modulo 2: '1' becomes 1, '0' becomes 0
f % Find: push array of 1-based indices of nonzeros
q % Subtract 1 from each entry
W % 2 raised to each entry
s % Sum of array. Implicitly display
JavaScript, 18 83 bytes
f=n=>parseInt(n,2)
f=n=>n.split('').reverse().reduce(function(x,y,i){return(+y)?x+Math.pow(2,i):x;},0)
Demo
f=n=>n.split('').reverse().reduce(function(x,y,i){return(+y)?x+Math.pow(2,i):x;},0)
document.write(f('1011')) // 11
Mathematica, 44 bytes
2^Range[Length[d=IntegerDigits@#]-1,0,-1].d&
Unnamed function taking an integer argument (interpreted as a base-10 integer, but will only have the digits 0 and 1) and returning an integer. d is set equal to the set of digits, and then the dot product of d with the appropriate sequence of powers of 2 generates the value.
C, 53
v(char*s){int v=0,c;while(c=*s++)v+=v+c-48;return v;}
Same as my javascript answer
Test Ideone
Turing Machine Code, 272 bytes
(Using, as usual, the morphett.info rule table syntax)
0 * * l B
B * * l C
C * 0 r D
D * * r E
E * * r A
A _ * l 1
A * * r *
1 0 1 l 1
1 1 0 l 2
1 _ * r Y
Y * * * X
X * _ r X
X _ _ * halt
2 * * l 2
2 _ _ l 3
3 * 1 r 4
3 1 2 r 4
3 2 3 r 4
3 3 4 r 4
3 4 5 r 4
3 5 6 r 4
3 6 7 r 4
3 7 8 r 4
3 8 9 r 4
3 9 0 l 3
4 * * r 4
4 _ _ r A
AKA "Yet another trivial modification of my earlier base converter programs."
Try it online, or you can also use test it using this java implementation.
JavaScript (ES6), 38
Simple is better
s=>eval("for(i=v=0;c=s[i++];)v+=+c+v")
Test
f=s=>eval("for(i=v=0;c=s[i++];)v+=+c+v")
console.log("Test 0 to 99999")
for(e=n=0;n<100000;n++)
{
b=n.toString(2)
r=f(b)
if(r!=n)console.log(++e,n,b,r)
}
console.log(e+" errors")
Jelly, 10 bytes
DLḶ2*U,DPS
You know your Jelly code is still golfable when it's above 7 bytes...
It basically consists of two parts
2* generate a list of the powers of two
LḶ for all the powers of 2 from 0 to the length of the binary input
D Convert the binary string into a list to get its length with L
U Then upend that list (for '101010', we now have a list of [32, 16, 8, 4, 2, 1]
, Combine this list
D with the individual digits of the input
P multiply them with eah other [32*1, 16*0, 8*1, 4*0, 2*1, 1*0]
S And sum the result 42 = 32 + 0 + 8 + 0 + 2 + 0
PHP, 44 bytes
for(;""<$c=$argv[1][$i++];)$n+=$n+$c;echo$n;
I could have sworn that I´ve seen that question before. But well.
Reads the number from left to right, shifts left and adds the current bit.
Pyke, 10 9 bytes
1QY_%0m@s
QY_ - reverse digits input
1 % - get indecies with a value of 1
0m@ - map(set_nth_bit(0, i), ^)
s - sum(^)
Also 9 bytes
Y_'XltV}+
Y_ - reverse digits
'Xlt - splat(^), len(^)-1
V - repeat length times:
}+ - double and add to stack
Java, 84 79 46 48 bytes
- Version 3.1
Changed to long/48 bytes:
s->{long x=0;for(char c:s)x=c-48l+x*2;return x;}
- Version 3.0
Did some golfing/46 bytes:
s->{int x=0;for(char c:s)x=c-48+x*2;return x;}
- Version 2.0
Thanks to @Geobits!/79 bytes:
s->{int i=Math.pow(2,s.length-1),j=0;for(char c:s){j+=c>48?i:0;i/=2;}return j;}
- Version 1.0
84 bytes:
s->{for(int i=-1,j=0;++i<s.length;)if(s[i]>48)j+=Math.pow(2,s.length-i+1);return j;}
Java 7, 87 bytes
long c(String b){int a=b.length()-1;return a<0?0:b.charAt(a)-48+2*c(b.substring(0,a));}
For some reason I always go straight to recursion. Looks like an iterative solution works a bit nicer in this case...
JavaScript (ES7), 56 47 bytes
Reverses a binary string, then adds each digit's value to the sum.
n=>[...n].reverse().reduce((s,d,i)=>s+d*2**i,0)
Demo
f=n=>[...n].reverse().reduce((s,d,i)=>s+d*2**i,0)
document.write( f('101010') ) // 42
Brain-Flak, 46, 28 bytes
([]){{}({}<>({}){})<>([])}<>
Many many bytes saved thanks to @Riley!
Since brain-flak can't take binary input, input is a list of '0's and '1's.
Explanation:
#Push the height of the stack
([])
#While true:
{
#Pop the height of the stack
{}
#Push this top number to (the other stack * 2)
({}<>({}){})
#Toggle back on to the main stack
<>
#Push the new height of the stack
([])
#endwhile
}
#Toggle back to the other stack, implicitly display.
<>
k, 8 bytes
Same method as the Haskell answer above.
{y+2*x}/
Example:
{y+2*x}/1101111111010101100101110111001110001000110100110011100000111b
2016120520371234567
PowerShell v2+, 55 bytes
param($n)$j=1;$n[$n.length..0]|%{$i+=+"$_"*$j;$j*=2};$i
Feels too long ... Can't seem to golf it down any -- tips appreciated.
Explanation
param($n)$j=1;$n[$n.length..0]|%{$i+=+"$_"*$j;$j*=2};$i
param($n)$j=1; # Take input $n as string, set $j=1
$n[$n.length..0] # Reverses, also converts to char-array
|%{ }; # Loop over that array
$i+=+"$_"*$j; # Increment by current value of $j times current digit
$j*=2 # Increase $j for next loop iteration
$i # Leave $i on pipeline
# Implicit output
05AB1E, 6 bytes
Code:
$¦v·y+
For the explantion, let's take the example 101010. We start with the number 1 (which is represented by the first digit). After that, we have two cases:
- If the digit is a 0, multiply the number by 2.
- If the digit is a 1, multiply the number by 2 and add 1.
So for the 101010 case, the following is calculated:
- 101010, start with the number 1.
- 101010, multiply by two, resulting into 2.
- 101010, multiply by two and add one, resulting into 5.
- 101010, multiply by two, resulting into 10.
- 101010, multiply by two and add one, resulting into 21.
- 101010, multiply by two, resulting into 42, which is the desired result.
Code explanation:
$ # Push 1 and input
¦ # Remove the first character
v # For each character (starting with the first)
· # Multiply the carry number by two
y+ # Add the current character (converted automatically to a number)
Uses the CP-1252 encoding. Try it online!
Bash + GNU utilities, 29 bytes
sed 's/./2*&+/g;s/.*/K&p/'|dc
I/O via stdin/stdout.
The sed expression splits the binary up into each digit and builds a RPN expression for dc to evaluate.
PHP, 64 bytes
foreach(str_split(strrev($argv[1]))as$k=>$v)$t+=$v*2**$k;echo$t;
We reverse our binary number, split it into its component digits, and sum them based on position.
T-SQL, 202 Bytes
DECLARE @b varchar(max)='1',@ int=1 declare @l int=LEN(@b)declare @o bigint=CAST(SUBSTRING(@b,@l,1)AS bigint)WHILE @<@l BEGIN SET @o=@o+POWER(CAST(SUBSTRING(@b,@l-@,1)*2AS bigint),@)SET @=@+1 END PRINT @o
Retina, 15 bytes
Converts from binary to unary, then unary to decimal.
1
01
+`10
011
1




