Altherat
Altherat

Reputation: 701

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.

Upvotes: 0

Views: 53

Answers (1)

Arasn
Arasn

Reputation: 158

You seem to have answer already.

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

Upvotes: 1

Related Questions