# I don't get how the following expression gets simplified

This is a discussion on I don't get how the following expression gets simplified within the C Programming forums, part of the General Programming Boards category; I was told the following Code: if ( (a && b) || (!a && !b) ) was equivalent to Code: ...

1. ## I don't get how the following expression gets simplified

I was told the following
Code:
`if ( (a && b) || (!a && !b) )`
was equivalent to

Code:
`if(!!a == !!b)`
Can someone explain to me how they arrive at this answer? I really don't see the steps involved.

2. Originally Posted by Overworked_PhD
I was told the following
Code:
`if ( (a && b) || (!a && !b) )`
was equivalent to

Code:
`if(!!a == !!b)`
Can someone explain to me how they arrive at this answer? I really don't see the steps involved.
The first expression evaluates to true if both a and b are true, or if neither a nor b are true. Since any non-zero value is true, then you cannot just condense this to "a == b". But if you double-negate, then a true value negates to zero, then negates to one, and then the comparison will work.

If only we had a logical XOR (as opposed to bitwise), we could say:

Code:
`if( ! ( a ^^ b ) )`
But alas we don't.

3. Okay. I get it.

4. It could be further simplified to either:

!a == !b

- or -

!( !a ^ !b )

5. Originally Posted by Sebastiani
It could be further simplified to either:

!a == !b

- or -

!( !a ^ !b )
The first one is going to be the faster of those two, I'm pretty sure.

--
Mats