| Bytes | Lang | Time | Link |
|---|---|---|---|
| 001 | Vyxal 3 | 250425T101922Z | Themooni |
| 031 | Tcl | 250425T093143Z | sergiol |
| 042 | AWK | 241205T060808Z | xrs |
| 055 | Forth gforth | 240808T161855Z | david |
| 027 | TIBASIC | 240517T110256Z | Sylveste |
| 013 | TIBasic | 240524T161831Z | MarcMush |
| 021 | PowerShell Core | 240524T035749Z | Julian |
| 012 | Perl 5 p | 240522T192413Z | Xcali |
| 004 | dc | 240522T175349Z | Digital |
| 025 | MATLAB | 240517T114434Z | rst |
| 009 | Retina 0.8.2 | 240517T095430Z | Neil |
| nan | 240517T093038Z | slow-jau | |
| 038 | C gcc | 240516T144238Z | badatgol |
| 020 | Google Sheets | 240516T214108Z | doubleun |
| 002 | MathGolf | 240517T073645Z | Kevin Cr |
| 002 | 05AB1E | 240517T072205Z | Kevin Cr |
| 004 | Charcoal | 240516T150025Z | Neil |
| 001 | APL dzaima/APL | 240516T131914Z | Adá |
| 015 | PARI/GP | 240517T024035Z | alephalp |
| nan | C++ gcc | 240516T132457Z | Someone |
| 078 | Dart | 240517T011927Z | Redz |
| 1725 | Python 2 | 240516T221711Z | Jonathan |
| 018 | JavaScript ES6 | 240516T130704Z | Arnauld |
| 018 | Ruby | 240516T212122Z | AZTECCO |
| 029 | Python 3 | 240516T141252Z | Jitse |
| 020 | Funge98 | 240516T171745Z | Alt Shif |
| 003 | APL+WIN | 240516T131851Z | Graham |
Vyxal 3, 1 byte
b
really thought i'd have to massage it more or choose an edgy input format but turns out the builtin is exactly right.
<script type="vyxal3">
b
</script>
<script>
args=[["[0, 1, 1, 0, 1, 0]"],["[1, 1, 0, 1, 0, 0, 1, 1, 0]"]]
</script>
<script src="https://themoonisacheese.github.io/snippeterpreter/snippet.js" type="module"/>
Forth (gforth), 55 bytes
: a 2 BASE ! DEPTH 1 +DO SWAP I LSHIFT + LOOP DECIMAL ;
Assuming we start with DECIMAL base and return to it.
TI-BASIC, 30 28 27 bytes
Input A
dim(ʟA
Σ(2^(Ans-I)ʟA(I),I,1,Ans
Takes an array of bits as input, and outputs the converted unsigned integer. Here are the example inputs and outputs used in the challenge:
An interesting side effect of this particular algorithm is that the numbers in the input list don't have to be binary digits for the code to complete successfully.
MATLAB, 25 bytes
Built-in function bin2dec() takes character arrays, so we need to convert from a logical array.
For golfing, we save a handle to an anonymous function.
f=@(b)bin2dec(char(48+b))
Test
>> f=@(b)bin2dec(char(48+b))
f =
function_handle with value:
@(b)bin2dec(char(48+b))
>> bb = '1'==dec2bin( 42 )
bb =
1×6 logical array
1 0 1 0 1 0
>> f(bb)
ans =
42
Retina 0.8.2, 9 bytes
+1`¶
$`
1
Try it online! Takes each bit on its own line, MSB first. Explanation:
1`¶
$`
Replace the first newline with the string on the left, thus both doubling it and also concatenating it with the second line.
+`
Repeat until only one line is left.
1
Count the resulting number of 1s.
Retina 1 can do this with arbitrary precision in 20 bytes:
+`^.+¶(.)
$.($1*_2**
Try it online! Takes each bit on its own line, MSB first. Explanation:
^.+¶(.)
$.($1*_2**
Double the first value and add the second value. ($1*_ converts the second value to unary, 2** is shorthand for 2*$&_ which converts the first value to unary and doubles it, then $.( serves both to take the sum but also to sidestep the unary conversion, doing the arithmetic in decimal.)
+`
Repeat until there is only one value left.
C (gcc), 38 bytes
-6 bytes thanks to @AZTECCO
f(a,n)int*a;{n=n--?f(a+1,n)|*a<<n:*a;}
A port from this answer. I tried to use memcpy() but to no avail. If someone could do that it will be pretty cool (and also help me from losing sleep).
Google Sheets, 20 bytes
=bin2dec(join(,A:A))
Put the array of bits in column A1:A as zeros and ones, and the formula in cell B1.
MathGolf, 2 bytes
xä
Could have been 1 byte if we'd be allowed to input as a string instead of bit-array:
å
Explanation:
x # Reverse the (implicit) input-list
ä # Convert from a reversed binary list to a base-10 integer
# (after which the entire stack joined together is output implicitly as result)
å # Convert the (implicit) input-string from a binary string to a base-10 integer
# (after which the entire stack joined together is output implicitly as result)
Don't ask me why ä and â work with reversed binary lists, because I honestly don't know.
Minor note: unlike my 05AB1E answer, a join + convert from binary-string combination as alternative would be 3 bytes instead of 2 in MathGolf, since joining a list of integers/digits will result in a new integer, so an explicit cast to string is necessary (either before or after the join) before å can be used: y░å or ░yå.
05AB1E, 2 bytes
2β
Try it online or verify all test cases.
Or alternatively:
JC
Try it online or verify all test cases.
Could have been 1 byte if we'd be allowed to input as a string instead of bit-array:
C
Try it online or verify all test cases.
Explanation:
2β # Convert the (implicit) input-list from a base-2 list to a base-10 integer
# (after which this integer is output implicitly as result)
J # Join the (implicit) input-list together
C # Convert it from a binary string to a base-10 integer
# (after which this integer is output implicitly as result)
C # (same as above, but on the input-string directly without a `J`oin beforehand)
Charcoal, 4 bytes
I⍘S²
Try it online! Link is to verbose version of code. Takes input as a string of 0s and 1s from MSB to LSB. Explanation:
S Input string
⍘ Base conversion
² Literal integer `2`
I Cast to string
Implicitly print
Alternatively, taking input as an array of bits from MSB to LSB is also 4 bytes:
I↨A²
Try it online! Link is to verbose version of code. Explanation:
A Input array
↨ Base conversion
² Literal integer `2`
I Cast to string
Implicitly print
APL (dzaima/APL), 1 byte
⊥
Fun fact: This exact functionality was available in APL's precursor already in 1962.
PARI/GP, 15 bytes
a->Pol(a)%(x-2)
Converts the input list to a polynomial, and then evaluates it at x=2.
C++ (gcc), 85 81 80 74 72 bytes (-4 because of badatgolf) (-1) (-6 due to yksisarvinen) (-2 because of badatgolf again)
I'm certain there are ways to optimize this, but I'm pretty proud of it. It takes input as ones and zeros in sequence. (The TIO comes with input.)
#import<iostream>
int x,y;main(){while(std::cin>>x)y+=y+x;std::cout<<y;}
Restrictions (:nerd:)
int is the shortest appropriate type name in C++. This means the program only outputs a positive integer with at most 31 bits, and if there are exactly 32 bits, the integer will be negative as defined by two's complement.
Also, #import is deprecated, and only now do I realize how ridiculous golfing is.
AND there's no int for main. That's TWO WARNINGS.
Dart, 78 bytes
int f(var l){int x=0;for(var i=0;i<l.length;i++)x+=(x+l[i]).toInt();return x;}
Needs a list of 0s and 1s to work.
Python 2, (17?) 25 bytes
lambda b:int(`b`[1::3],2)
An unnamed function that accepts a non-empty list of integers from \$(0,1)\$ and returns an integer.
If we may accept a non-empty string of 0 and 1 characters, 17 bytes: lambda b:int(b,2)
Python 3, (17?) 28 bytes
lambda b:int(str(b)[1::3],2)
An unnamed function that accepts a non-empty list of integers from \$(0,1)\$ and returns an integer.
If we may accept a non-empty string of 0 and 1 characters, 17 bytes: lambda b:int(b,2)
JavaScript (ES6), 18 bytes
Expects an array of binary digits.
a=>'0b'+a.join``-0
JavaScript (ES6), 26 bytes
Expects an array of either binary digits or Booleans.
a=>a.map(b=>n=n*2|b,n=0)|n
Python 3, 29 bytes
lambda a:int('%d'*len(a)%a,2)
Python 3, 33 bytes
f=lambda a:a>[]and a.pop()+2*f(a)
-3 bytes thanks to Mukundan314
Funge-98, 20 bytes
&:1j3_\.@j3:-1\+&*2\
First reads the length of the array, then reads the array itself as a series of binary digits.
APL+WIN, 17 12, 9, 3 bytes
-5 bytes thanks to Adám.
Prompts for input as either boolean or 'true' 'false' as per original question.
2⊥(↑¨⎕)∊'t'1
Try it online! Thanks to Dyalog Classic
With input restricted to 'true' 'false' reduces to
2⊥'t'=↑¨⎕
and if input restricted to boolean reduces further to
2⊥⎕

