简体   繁体   中英

Possible to simplify this conditional statement?

Is it possible to simplify the following statement using && or ||, or even & or | or ^?

int result = A ? (B ? 1 : -1) : (B ? -1 : 1);

I believe I could assign a temporary variable like so:

boolean C = B ? A : !A; // Also: is it correct that this can be simplified to !(A ^ B)?

And then do:

int result = C ? 1 : -1;

But I'm curious if it's possible without a temporary variable.

您似乎已经有了答案。

int result = !(A ^ B) ? 1:-1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM