Unary and binary operators

Vector data types can use some of the unary operators and binary operators that are used with primitive data types. Note that all operators require compatible types as operands unless otherwise stated. These operators are not supported at global scope or for objects with static duration, and there is no constant folding.

Unary operators

For unary operators, each element in the vector has the operation applied to it.

Table 1. Unary operators
Operator Integer vector types Vector double Bool vector types
++ Yes Yes No
−− Yes Yes No
+ Yes Yes No
Yes (except unsigned vectors) Yes No
~ Yes No Yes
*1 Yes Yes Yes
Note:
  1. Although the preferred alignment for vector data types is at a multiple of 16 bytes, pointers can point to vector objects at any alignment.
Binary operators

For binary operators, each element has the operation applied to it with the same position element in the second operand. Binary operators also include relational operators. For relational operators, each element has the operation applied to it with the same position element in the second operand and the results have the AND operator applied to them to get a final result of a single value.

Table 2. Binary operators
Operator Integer vector types Vector double Bool vector types
= Yes Yes Yes
+ Yes Yes No
Yes Yes No
* Yes Yes No
/ Yes Yes No
% Yes No No
& Yes No Yes
| Yes No Yes
^ Yes No Yes
<< Yes No Yes
>> Yes No Yes
[]1 Yes Yes Yes
== Yes Yes Yes
!= Yes Yes Yes
< Yes Yes No
> Yes Yes No
<= Yes Yes No
>= Yes Yes No
Note:
  1. The [] operator returns the vector element at the position specified. If the position specified is outside of the valid range, the behavior is undefined.
For the following code:
vector unsigned int a = {1,2,3,4};
vector unsigned int b = {2,4,6,8};
vector unsigned int c = a + b;
int e = b > a;
int f = a[2];
vector unsigned int d = ++a;
c would have the value (3,6,9,12), d would have the value (2,3,4,5), e would have a non-zero value, and f would have the value 3.


Voice your opinion on getting help information Ask IBM compiler experts a technical question in the IBM XL compilers forum Reach out to us