next up previous contents
Next: Comparison operators on vectors Up: Vectors and vector operators Previous: Vector coercion operator   Contents

Arithmetic on vectors

Arithmetic operators applied to vectors treat the vectors as unsigned binary numbers. As stated above, if the vectors are of unequal length, the shorter vector is prepended with a vector of zeros to make the lengths equal. Thus

        [0,1,1,0] + [1,0]
is equivalent to

        [0,1,1,0] + [0,0,1,0]

The arithmetic operator is then applied to the unsigned binary numbers represented by the two vectors, yielding an unsigned binary representation of the result, of the same length as the argument vectors. The operators are the same as they are on integers, except the result is modulo $2^n$, where $n$ is the vector length. The result is always negative (in the range $0\ldots(2^n-1)$). For example,

           [0,1,1,0]
         + [0,0,1,0]
         = [1,0,0,0]
and

           [1,1,1,0]
         + [0,0,1,0]
         = [0,0,0,0]

Note that, since the arithmetic is modular, it doesn't actually matter whether we look at it as signed or unsigned, except that extension is always by zeros. This extension by zeros implicitly treats vectors as unsigned numbers.



2002-10-28