11
\$\begingroup\$

Task is simple!
Take an array of binary values (can be boolean, 0/1, etc.) and use those bits to construct an n-th bit long unsigned integer (return type does not need to be an unsigned integer, just a number of any form)
Examples:
[0, 1, 1, 0, 1, 0]
->
26
[true, true, false, true, false, false, true, true, false]
->
422

Functions are treated as the program e.x.

function uint(input) {
   /* code here */
   return result;
}

is the same as

input = getInput();
/* code here */
print(result);

Smallest code in bytes wins

\$\endgroup\$
10
  • 1
    \$\begingroup\$ Welcome to the site! Please note that bonuses in code golf are not recommended. You are also making an assumption about language features (they have an explicit Boolean type). \$\endgroup\$
    – Arnauld
    Commented May 16 at 13:05
  • 1
    \$\begingroup\$ Sorry about that! I can remove the bonus feature. I noticed that other challenges often had bonuses. \$\endgroup\$ Commented May 16 at 13:14
  • 1
    \$\begingroup\$ It doesn't need to return an unsigned integer, just a number. I can make that clearer if needed. By unsigned integer I mean it should treat the bits AS an unsigned integer. It does not need to return an unsigned integer, integer, etc. \$\endgroup\$ Commented May 16 at 13:16
  • 1
    \$\begingroup\$ So it's just "return any value defined by these given bits in your language"? \$\endgroup\$
    – Someone
    Commented May 16 at 13:44
  • 5
    \$\begingroup\$ Isn't this a duplicate of Binary to decimal converter? \$\endgroup\$
    – Luis Mendo
    Commented May 16 at 22:23

23 Answers 23

6
\$\begingroup\$

APL (dzaima/APL), 1 byte

Try it online!


Fun fact: This exact functionality was available in APL's precursor already in 1962.

\$\endgroup\$
5
\$\begingroup\$

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⊥⎕    
\$\endgroup\$
8
  • \$\begingroup\$ There's no reason to parse the text form (which isn't a Boolean array anyway) so a simple 2⊥⎕ will suffice. \$\endgroup\$
    – Adám
    Commented May 16 at 13:19
  • \$\begingroup\$ But if you really want to, then 2⊥'t'1∊⍨↑¨⎕ is much shorter. \$\endgroup\$
    – Adám
    Commented May 16 at 13:21
  • \$\begingroup\$ @Adám Thanks but I am afraid my ancient version of APL+WIN does not support the functionality you have recommended. The first gives a domain error and for the second I do not have the ⍨ operator. \$\endgroup\$
    – Graham
    Commented May 16 at 13:25
  • \$\begingroup\$ Fair enough, but 2⊥(↑¨⎕)∊'t'1 is still shorter. That said, (a newer version of) APL+Win does have , as you can see here. \$\endgroup\$
    – Adám
    Commented May 16 at 14:08
  • \$\begingroup\$ @Adám Thanks again. I note the version of APL+WIN you referenced is 19. My ancient version is 5! \$\endgroup\$
    – Graham
    Commented May 16 at 14:15
5
\$\begingroup\$

JavaScript (ES6), 18 bytes

Expects an array of binary digits.

a=>'0b'+a.join``-0

Try it online!


JavaScript (ES6), 26 bytes

Expects an array of either binary digits or Booleans.

a=>a.map(b=>n=n*2|b,n=0)|n

Try it online!

\$\endgroup\$
0
4
\$\begingroup\$

Python 3, 29 bytes

lambda a:int('%d'*len(a)%a,2)

Try it online!

Python 3, 33 bytes

f=lambda a:a>[]and a.pop()+2*f(a)

Try it online!

-3 bytes thanks to Mukundan314

\$\endgroup\$
2
  • \$\begingroup\$ -3 bytes for the recursive variant \$\endgroup\$ Commented May 16 at 16:59
  • \$\begingroup\$ @Mukundan314 brilliant, thanks! \$\endgroup\$
    – Jitse
    Commented May 16 at 17:31
4
\$\begingroup\$

C (gcc), 38 bytes

-6 bytes thanks to @AZTECCO

f(a,n)int*a;{n=n--?f(a+1,n)|*a<<n:*a;}

Try it online!

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).

\$\endgroup\$
2
  • \$\begingroup\$ you'll need to initialize y for the function to be reusable \$\endgroup\$
    – att
    Commented May 17 at 3:02
  • \$\begingroup\$ sorry I forgot This \$\endgroup\$
    – AZTECCO
    Commented May 17 at 8:44
3
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ Oh hey, welcome to the code golf side of the stack exchange! \$\endgroup\$
    – lyxal
    Commented May 17 at 22:42
  • \$\begingroup\$ Aha yes. Hi lyxal! I've descended! (I can't golf very well but I might as well.) \$\endgroup\$
    – Redz
    Commented May 18 at 1:52
2
\$\begingroup\$

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.

Try it online!

\$\endgroup\$
8
  • 1
    \$\begingroup\$ 57 bytes if you change your program to use a function instead.Try it online! \$\endgroup\$
    – badatgolf
    Commented May 16 at 14:11
  • 2
    \$\begingroup\$ not really, I just changed the input method and golf up a few parts. I recommend you to look at tips for golfing at C++ and C (for example, you can use #import instead of #include, y*2 instead of y<<1, and so on. \$\endgroup\$
    – badatgolf
    Commented May 16 at 14:46
  • 1
    \$\begingroup\$ my brain told me bitshifts would be better for some reason. \$\endgroup\$
    – Someone
    Commented May 16 at 15:15
  • 1
    \$\begingroup\$ You can get rid of int before main(), gcc will happily accept it with a warning. And moving int x,y before main saves another two bytes, global variables are zero-initialised. \$\endgroup\$ Commented May 16 at 23:13
  • 1
    \$\begingroup\$ @badatgolf as a note , my suggestion for the C answer also works here for 44 \$\endgroup\$
    – AZTECCO
    Commented May 18 at 7:44
2
\$\begingroup\$

PARI/GP, 15 bytes

a->Pol(a)%(x-2)

Attempt This Online!

Converts the input list to a polynomial, and then evaluates it at x=2.

\$\endgroup\$
2
\$\begingroup\$

Perl 5 -p, 12 bytes

$_=oct"0b$_"

Try it online!

\$\endgroup\$
2
\$\begingroup\$

TI-BASIC, 30 28 27 bytes

  • Removed 2 bytes thanks to MarcMush
  • Removed 1 more byte thanks again to MarcMush
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:

input is a list of bits, output is an unsigned integer

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.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ you should be able to use Ans instead of D to save 2 bytes \$\endgroup\$
    – MarcMush
    Commented May 17 at 17:28
  • \$\begingroup\$ Also one more byte by using a different list name codegolf.stackexchange.com/a/143898 \$\endgroup\$
    – MarcMush
    Commented May 22 at 11:05
  • \$\begingroup\$ @MarcMush Unfortunately, those shorter list names don't work when modifying an item of the list. To quote the answer, "1→ᶫA(3 cannot be changed to 1→A(3" :/ \$\endgroup\$ Commented May 22 at 14:59
  • \$\begingroup\$ I mean Input A instead of Input L₁. Then of course you need to call then elements with ᶫA(I) but it's the same amount of bytes since L₁ is 2 bytes \$\endgroup\$
    – MarcMush
    Commented May 24 at 15:56
  • \$\begingroup\$ Ohh I see, thanks! I'll update it \$\endgroup\$ Commented May 24 at 20:31
1
\$\begingroup\$

Ruby, 18 bytes

->b{(b*"").to_i 2}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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.

Try it online!

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.

Try it online!

If we may accept a non-empty string of 0 and 1 characters, 17 bytes: lambda b:int(b,2)

\$\endgroup\$
1
\$\begingroup\$

MathGolf, 2 bytes

Try it online.

Could have been 1 byte if we'd be allowed to input as a string instead of bit-array:

å

Try it online.

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å.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Reversed binary lists make more sense if you want to index the list because the bit at index n has value 2ⁿ. \$\endgroup\$
    – Neil
    Commented May 17 at 9:56
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

Uiua

Without using a builtin (10 bytes):

/+×ⁿ:2⇡⧻.⇌

With builtins (3 bytes):

°⋯⇌

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Nice! You can do it without bits in 7 bytes: /+ⁿ:2⊚⇌. Also, in order to use bytes instead of chars, you need to specify how they are being stored as bytes. I have made (and sometimes update) a SBCS (byte encoding system) for Uiua: tinyurl.com/Uiua-SBCS-Apr-12 \$\endgroup\$
    – Tbw
    Commented May 17 at 18:49
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ You can save 1 byte using [... ''] instead of char(...); see here \$\endgroup\$
    – Luis Mendo
    Commented May 19 at 16:13
1
\$\begingroup\$

dc, 4 bytes

2i?p

Reads an array of 0 or 1 chars from stdin.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

TI-Basic, 13 bytes

sum(Ans2^(dim(Ans)-cumSum(Ans!

takes input as Ans (last result)

enter image description here

\$\endgroup\$
0
\$\begingroup\$

Funge-98, 20 bytes

&:1j3_\.@j3:-1\+&*2\

Try it online!

First reads the length of the array, then reads the array itself as a series of binary digits.

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 2 bytes

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)
\$\endgroup\$
0
\$\begingroup\$

PowerShell Core, 21 bytes

$args|%{$s+=$_+$s}
$s

Try it online!

Takes an array, returns an integer

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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