m1au
m1au

Reputation: 107

hlsl syntax conditional expression

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);?

Upvotes: 2

Views: 656

Answers (1)

MooseBoys
MooseBoys

Reputation: 6793

(x != y != z) is not the same as (x != y && y != z). In general, follows the same rules as . 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 bools or can guaranteed to be either 0 or 1, the expression reduces to the logical exclusive-or (xor) of the three terms.

Upvotes: 1

Related Questions