function bits = dec2bitv(d,n)
% DEC2BITV Convert a decimal integer to a bit vector.
% bits = dec2bitv(d,n) is just like the built-in dec2bin, except the answer is a vector, not a string.
% n is an optional minimum length on the bit vector.
% If d is a vector,  each row of the output array will be a bit vector.

%if nargin==0, error('Not enough input arguments.'); end
%if isempty(d), s = []; return, end

d = d(:); % Make sure d is a column vector.

if (nargin<2)
  n=1; % Need at least one digit even for 0.
else
  if prod(size(n))~=1 | n<0, error('N must be a positive scalar.'); end
  n = round(n); % Make sure n is an integer.
end;

[f,e]=log2(max(d)); % How many digits do we need to represent the numbers?
bits=rem(floor(d*pow2(1-max(n,e):0)),2);
