a(n) = {
	my (nb0=0, nb1=0);
	while (1,
		if (n==0,
				return (2^(nb0+nb1)+2^nb1-1),
			n%2,
				nb1++,
				nb0++;
				if (nb1,
					return (n\2*2^(nb0+nb1)+2^(nb0+nb1-1)+2^(nb1-1)-1)
				)
		);
		n\=2;
	);
}

for (n=1, 10 000, print (n " " a(n)))

quit