%converts a (base-10) number a, with 0<=a<=1, into 
%base 2 expansion with a maximum of n digits

a = input('convert this number in [0,1] to base 2: ') %convert a to base 2
n = 10;                              %maximum length of base 2 expansion
b = zeros(1,n);                      %base 2 expansion of a with n digits

x = a;
for i=1:n
    if 0<=x && x<2^(-i)
        b(i) = 0;
    else
        b(i) = 1;
    end
    x=x-b(i)*2^(-i);
end
b
        