Sign in to follow this  
danromeo

HLSL boolean "?" operator ?

Recommended Posts

danromeo    238
Hello.

Microsoft lists a HLSL boolean "?" operator here: [url="http://msdn.microsoft.com/en-us/library/bb509631(v=vs.85).aspx#Boolean_Math_Operators"]http://msdn.microsof..._Math_Operators[/url] but then gives absolutely no information about it, examples or otherwise.

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

Thanks

Share this post


Link to post
Share on other sites
n3Xus    951
It works like it does in a normal programming language, so you probably never used it before anywhere [img]http://public.gamedev.net/public/style_emoticons/default/laugh.gif[/img]

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

Share this post


Link to post
Share on other sites
[quote name='danromeo' timestamp='1307286067' post='4819760']
Hello.

Microsoft lists a HLSL boolean "?" operator here: [url="http://msdn.microsoft.com/en-us/library/bb509631(v=vs.85).aspx#Boolean_Math_Operators"]http://msdn.microsof..._Math_Operators[/url] but then gives absolutely no information about it, examples or otherwise.

Does anybody know what this is and how to use it?
[/quote]

It's actually the [b]?: [/b]operator. In C++, and I'm guessing HLSL too, the [url="http://en.wikipedia.org/wiki/Ternary_operation"]ternary operator[/url] is used like [url="http://en.wikipedia.org/wiki/%3F:"]this[/url]:

[b](boolean-statement) ? (result-if-true) : (result-if-false)[/b]

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

[code]if(boolean-statement)
return result-if-true;
else
return result-if-false;[/code]

Example:

[code]bool lastNameFirst;
std::cout << (lastNameFirst? "Henry, John" : "John Henry");[/code]

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

[code]int maxValue = (valueA > valueB ? valueA : valueB);[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this