# Addition and Subtraction Operators

The addition and subtraction operators return the class of the nearest common ancestor when the right-side operands are of different signs. The following code provides examples of usage and miscellaneous exceptions.

Syntax Usage for Addition and Subtraction Operators

Return nearest common ancestor type, I16vec4.

Is16vec4 A;

Iu16vec4 B;

I16vec4 C;

`C = A + B;`

Returns type left-hand operand type.

Is16vec4 A;

Iu16vec4 B;

`A += B;`

`B -= A;`

Explicitly convert `B` to Is16vec4.

`Is16vec4 A,C;`

`Iu32vec24 B;`

`C = A + C;`

`C = A + (Is16vec4)B;`

Addition and Subtraction Operators with Corresponding Intrinsics

Operation | Symbols | Syntax | Corresponding Intrinsics
--- | --- | --- | ---
Addition | `+` `+=` | `R = A + B` `R += A` | _mm_add_epi64 _mm_add_epi32 _mm_add_epi16 _mm_add_epi8 _mm_add_pi32 _mm_add_pi16 _mm_add_pi8
Subtraction | `-` `-=` | `R = A - B` `R -= A` | _mm_sub_epi64 _mm_sub_epi32 _mm_sub_epi16 _mm_sub_epi8 _mm_sub_pi32 _mm_sub_pi16 _mm_sub_pi8

The following table lists addition and subtraction return values for combinations of classes when the right side operands are of different signedness. The two operands must be the same size, otherwise you must explicitly indicate the typecasting.

Addition and Subtraction Operator Overloading

Return Value | Available Operators | Right Side Operands
--- | --- | ---
R | Add | Sub | A | B
I64vec2 R | `+` | `-` | I[s|u]64vec2 A | I[s|u]64vec2 B
I32vec4 R | `+` | `-` | I[s|u]32vec4 A | I[s|u]32vec4 B
I32vec2 R | `+` | `-` | I[s|u]32vec2 A | I[s|u]32vec2 B
I16vec8 R | `+` | `-` | I[s|u]16vec8 A | I[s|u]16vec8 B
I16vec4 R | `+` | `-` | I[s|u]16vec4 A | I[s|u]16vec4 B
I8vec8 R | `+` | `-` | I[s|u]8vec8 A | I[s|u]8vec8 B
I8vec16 R | `+` | `-` | I[s|u]8vec2 A | I[s|u]8vec16 B

The following table shows the return data type values for operands of the addition and subtraction operators with assignment. The left side operand determines the size and signedness of the return value. The right side operand must be the same size as the left operand; otherwise, you must use an explicit typecast.

Addition and Subtraction with Assignment

Return Value ® | Left Side ® | Add | Sub | Right Side (A)
--- | --- | --- | --- | ---
I[x]32vec4 | I[x]32vec2 R | `+=` | `-=` | I[s|u]32vec4 A;
I[x]32vec2 R | I[x]32vec2 R | `+=` | `-=` | I[s|u]32vec2 A;
I[x]16vec8 | I[x]16vec8 | `+=` | `-=` | I[s|u]16vec8 A;
I[x]16vec4 | I[x]16vec4 | `+=` | `-=` | I[s|u]16vec4 A;
I[x]8vec16 | I[x]8vec16 | `+=` | `-=` | I[s|u]8vec16 A;
I[x]8vec8 | I[x]8vec8 | `+=` | `-=` | I[s|u]8vec8 A;

Parent topic: Integer Vector Classes