% frac2bin.m
% Conversion from a fractional number in (0, 1) to binary representation
% Input: f is a decimal number between (0, 1)
% Output: b is an array of bits (the MSB is the first element) 
% Example: frac2bin(.8125)
% ans = 1     1     0     1

% Author: Dr. David Pan, UAH

function b = frac2bin (f)

% Check for valid inputs
if (f <= 0) || (f >=1)
    error('Input must lie in [0, 1)!');
end

b = [];   % store the binary bits of the result

x = 1;
while(f > 0)        
    x = x * 0.5;
    if (f >= x)
        tmp = 1;
        f = f - x;
    else
        tmp = 0;
    end
    
    b = [b tmp];
end

 

        

