[Next] [Up] [Previous] [Contents]
Next: Comparison operators on vectors Up: Vectors and vector operators Previous: Vector coersion operator

Arithmetic on vectors

Arithmetic operators applied to vectors treat the vectors as unsigned binary numbers. 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 tex2html_wrap_inline940 , where n is the vector length, and the result is always positive (in the range tex2html_wrap_inline944 ). 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]

[N.B. 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. It would make sense to introduce a special kind of vector that sign-extends rather than zero-extending, to allow signed arithmetic. Or at least a sign-extension operator.]



Ken McMillan
Sat Jun 6 21:41:59 PDT 1998