# Construct a uInt from an array of bits

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

• 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). Commented May 16 at 13:05
• Sorry about that! I can remove the bonus feature. I noticed that other challenges often had bonuses. Commented May 16 at 13:14
• 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. Commented May 16 at 13:16
• So it's just "return any value defined by these given bits in your language"? Commented May 16 at 13:44
• Isn't this a duplicate of Binary to decimal converter? Commented May 16 at 22:23

# APL (dzaima/APL), 1 byte

⊥


Try it online!

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

# APL+WIN, 17 12, 9, 3 bytes

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⊥⎕

• There's no reason to parse the text form (which isn't a Boolean array anyway) so a simple 2⊥⎕ will suffice.
Commented May 16 at 13:19
• But if you really want to, then 2⊥'t'1∊⍨↑¨⎕ is much shorter.
Commented May 16 at 13:21
• @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. Commented May 16 at 13:25
• Fair enough, but 2⊥(↑¨⎕)∊'t'1 is still shorter. That said, (a newer version of) APL+Win does have ⍨, as you can see here.
Commented May 16 at 14:08
• @Adám Thanks again. I note the version of APL+WIN you referenced is 19. My ancient version is 5! Commented May 16 at 14:15

# 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!

# 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

• -3 bytes for the recursive variant Commented May 16 at 16:59
• @Mukundan314 brilliant, thanks! Commented May 16 at 17:31

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

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

# Charcoal, 4 bytes

Ｉ⍘Ｓ²


Try it online! Link is to verbose version of code. Takes input as a string of 0s and 1s from MSB to LSB. Explanation:

  Ｓ     Input string
⍘      Base conversion
²    Literal integer 2
Ｉ       Cast to string
Implicitly print


Alternatively, taking input as an array of bits from MSB to LSB is also 4 bytes:

Ｉ↨Ａ²


Try it online! Link is to verbose version of code. Explanation:

  Ａ     Input array
↨      Base conversion
²    Literal integer 2
Ｉ       Cast to string
Implicitly print


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

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

# C++ (gcc), 85818074 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!

• 57 bytes if you change your program to use a function instead.Try it online! Commented May 16 at 14:11
• 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. Commented May 16 at 14:46
• my brain told me bitshifts would be better for some reason. Commented May 16 at 15:15
• 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. Commented May 16 at 23:13
• @badatgolf as a note , my suggestion for the C answer also works here for 44 Commented May 18 at 7:44

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

# Perl 5-p, 12 bytes

$_=oct"0b$_"


Try it online!

# TI-BASIC, 3028 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:

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.

• you should be able to use Ans instead of D to save 2 bytes Commented May 17 at 17:28
• Also one more byte by using a different list name codegolf.stackexchange.com/a/143898 Commented May 22 at 11:05
• @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" :/ Commented May 22 at 14:59
• 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 Commented May 24 at 15:56
• Ohh I see, thanks! I'll update it Commented May 24 at 20:31

# Ruby, 18 bytes

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


Try it online!

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

# MathGolf, 2 bytes

xä


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

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

=bin2dec(join(,A:A))


Put the array of bits in column A1:A as zeros and ones, and the formula in cell B1.

# Uiua

Without using a builtin (10 bytes):

/+×ⁿ:2⇡⧻.⇌


With builtins (3 bytes):

°⋯⇌


Try it online!

• 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
– Tbw
Commented May 17 at 18:49

# 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. 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  • You can save 1 byte using [... ''] instead of char(...); see here Commented May 19 at 16:13 # dc, 4 bytes 2i?p  Reads an array of 0 or 1 chars from stdin. Try it online! # TI-Basic, 13 bytes sum(Ans2^(dim(Ans)-cumSum(Ans!  takes input as Ans (last result) # 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. # 05AB1E, 2 bytes 2β  Or alternatively: JC  Could have been 1 byte if we'd be allowed to input as a string instead of bit-array: C  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 Join beforehand)  # PowerShell Core, 21 bytes $args|%{$s+=$_+$s}$s
`

Try it online!

Takes an array, returns an integer