There is a one line solution:
Reduce(function(x,y) x*2+y, a)
Explanation:
Extending a Reduce application leads to something like:
Reduce(function(x,y) x*2+y, c(0,1,0,1,0)) = (((0*2 + 1)*2 + 0)*2 + 1)*2 + 0 = 10
With each new bit following it, we double the previously accumulated value and add the next bit to it.
Also see the description of the Reduce() function.
source share