In the book "Programming Vertex, Geometry, and Pixel Shaders" there is a small hlsl-script with the following instruction:
return (x != y != z);
Is this really allowed? Is this syntactically correct? What does this mean?
return (x != y && y != z);?
(x != y != z)
is not the same as (x != y && y != z)
. In general, hlsl follows the same rules as c. In this case, the left-to-right rule applies for the !=
operator. Assuming the values are integers, the expression (x != y != z)
is equivalent to the following:
int temp = (x != y); // true = 1, false = 0
int r = (temp != z); // true = 1, false = 0
As a result, the expression will evaluate to 1
if and only if x
and y
are equal and z
is not 0
, or if x
and y
are not equal and z
is not 1
.
If the values are bool
s or can guaranteed to be either 0
or 1
, the expression reduces to the logical exclusive-or (xor) of the three terms.