36

I have two variables which can be either true or false. I get these by doing query on database for presence or absence of certain product ids.

Now I need to set another variable which will be true or false. it will be true value when both the variables are true or both are false. It will be false value of one is true and other is false.

at present I take care of it using if statement

if ( v1 == true && v2 == true )
 result = true;
else if ( v1==false && v2 == false )
 result = true;
else if ( v1 == true && v2 == false )
 result = false;
else if ( v1==false && v2 == true )
 result = false;

Is there exists a better way of doing this ?

jzd
  • 23,473
  • 9
  • 54
  • 76
user679737
  • 361
  • 1
  • 3
  • 4

7 Answers7

155

I may be missing something very fundamental, but I'll give it a go:

result = ( v1 == v2 );
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
DXM
  • 4,413
  • 1
  • 19
  • 29
  • 10
    Actually the parentheses are even optional here, as the assignment operator nearly has the lowest precedence among all in those languages like Java, C++, C. – Benoit Mar 28 '11 at 07:43
  • 5
    What's up with Java answers related to booleans receiving so many upvotes (see also http://stackoverflow.com/questions/3907384/whats-the-most-concise-way-to-get-the-inverse-of-a-java-boolean-value/3907396#3907396)? :D – BoltClock Mar 28 '11 at 09:21
  • 3
    +1: lol, when I first read the question, i thought it could be a XOR question, but this make it simple and clear. – LiuYan 刘研 Mar 28 '11 at 18:55
  • @Benoit optional to the compiler, but we programmers forget compiler rules a lot! – corsiKa Mar 28 '11 at 21:55
  • @Benoit, did you just [Well Actually](http://tirania.org/blog/archive/2011/Feb-17.html) us? :P – Ruben Bartelink Mar 28 '11 at 22:20
  • @BoltClock Because if they were good at logical reasoning they wouldn't be using Java. I jest, I jest... Keep in mind I'm here because I couldn't figure this out myself. It's early and I need coffee. :P – RLH Mar 24 '17 at 13:38
  • The most elegant things are usually very simple things! Just used this brilliant answer to check if both text-fields have values or none of them does. – rav Aug 13 '21 at 11:18
15

You can use the logical XOR operator and logical NOT operator as:

result = !(v1^v2);
codaddict
  • 445,704
  • 82
  • 492
  • 529
5

This sort of problem, given a truth table, minimize the logic required to reproduce the truth values, is often nicely treated, with Karnaugh Maps

Your truth table here looks like:

 v1 v2  f(v1, v2)
  t  t     t
  t  f     f
  f  t     f
  f  f     t

And actually, as others have noted, given that truth table, a basic familiarity with logic should right away yield the function !xor

However, if you take the truth table and draw it as a Karnaugh Map, it looks like:


        v2
       f   t 
     ---------
 v  f| t | f |
 1  t| f | t |
     ---------

And the function looks like: !v1!v2 || v1v2 which if you look at 2 variable karnaugh map examples again can be seen to simplify to ! xor

Admittedly, 2 variable karnaugh maps are probably best treated with the ordinary logical operations by well, familiarity and memorization. But when expanding beyond 2 variables, Karnaugh maps are very illuminating -- you should look into them.

Jerry Asher
  • 836
  • 2
  • 10
  • 23
  • 1
    Interesting choice of upvoted solutions, I tend to think that result = !(v1 xor v2) is more clear than result = (v1 == v2). – Jerry Asher Mar 28 '11 at 07:32
  • 23
    I politely suggest that you get some more coffee … if you find `!(a ^ b)` to be clearer than `a == b` then you must still be asleep. And having a nightmare. – Konrad Rudolph Mar 28 '11 at 08:47
2

Use the XOR operator (^):

boolean result = !(v1 ^ v2)
Rune Aamodt
  • 2,551
  • 2
  • 23
  • 27
-2
if(v1 == v2) 
return true; 
else 
return false;
Lloyd Powell
  • 18,270
  • 17
  • 87
  • 123
Troydm
  • 2,642
  • 3
  • 24
  • 35
-2

The simple way is,if the two variables are equal then it should be true and if any one is false it is false. check this one.

if(v1 == v2)
    return true;
else 
    return false;
bluish
  • 26,356
  • 27
  • 122
  • 180
sreekanth
  • 1,267
  • 2
  • 13
  • 21
-3

Why not just compare the two?

if(v1 == v2) result = true;
vegasaurus
  • 180
  • 8