function [ b ] = BinaryRep( n ) % Determine binary representation of input natural number n. Output is a % vector of the binary digits, in descending order. % Get the number of digits: m = floor(log(n)/log(2)); % Prepare output: b = zeros(1,m+1); % Iteratively get the binary digits: p = n; i = 1; for k = m:-1:0 % Get integer part and remainder upon division by 2^k: q = floor(p/2^k); r = mod(p,2^k); % Set i-th digit equal to q. b(i) = q; i = i+1; % Update p: p = r; end end