Jump to content
  • Advertisement
Sign in to follow this  
danromeo

HLSL boolean "?" operator ?

This topic is 2572 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
It works like it does in a normal programming language, so you probably never used it before anywhere laugh.gif


int y=x<10?0:1 // == if(x<10) y= 0 else y=1

Share this post


Link to post
Share on other sites

Hello.

Microsoft lists a HLSL boolean "?" operator here: http://msdn.microsof..._Math_Operators but then gives absolutely no information about it, examples or otherwise.

Does anybody know what this is and how to use it?


It's actually the ?: operator. In C++, and I'm guessing HLSL too, the ternary operator is used like this:

(boolean-statement) ? (result-if-true) : (result-if-false)

It's a short-hand form of writing this:

if(boolean-statement)
return result-if-true;
else
return result-if-false;


Example:

bool lastNameFirst;
std::cout << (lastNameFirst? "Henry, John" : "John Henry");


Another example: (return the larger of the two values)

int maxValue = (valueA > valueB ? valueA : valueB);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!