function ubvec3 = ubvec_add ( n, ubvec1, ubvec2 )

%*****************************************************************************80
%
%% UBVEC_ADD adds two unsigned binary vectors.
%
%  Discussion:
%
%    A UBVEC is an integer vector of binary digits, intended to
%    represent a nonnegative integer.  UBVEC(1) is the units digit, UBVEC(N)
%    is the coefficient of 2^(N-1).
%
%  Example:
%
%    N = 4
%
%     UBVEC1       +  UBVEC2       =  UBVEC3
%
%    ( 1 0 0 0 )   + ( 1 1 0 0 )   = ( 0 0 1 0 )
%
%      1           +   3           =   4
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    19 November 2015
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the length of the vectors.
%
%    Input, integer UBVEC1(N), UBVEC2(N), the vectors to be added.
%
%    Output, integer UBVEC3(N), the sum of the two input vectors.
%
  overflow = 0;

  ubvec3(1:n) = ubvec1(1:n) + ubvec2(1:n);

  for i = n : -1 : 1
    while ( 2 <= ubvec3(i) )
      ubvec3(i) = ubvec3(i) - 2;
      if ( 1 < i )
        ubvec3(i-1) = ubvec3(i-1) + 1;
      else
        overflow = 1;
      end
    end
  end

  return
end
