# XOR Boolean

Discussion in 'Java' started by Buck Turgidson, Apr 5, 2004.

1. ### Buck TurgidsonGuest

Can someone check me on if the following two blocks are logically
equivalent? Is there an even better way to write this with a one-liner?

return (inverse ^ line_matched);

// and

if ( !inverse )
return line_matched;
else
return !line_matched;

Buck Turgidson, Apr 5, 2004

2. ### KristoffelGuest

Buck Turgidson wrote:
> Can someone check me on if the following two blocks are logically
> equivalent? Is there an even better way to write this with a one-liner?
>
>
>
> return (inverse ^ line_matched);
>
>
> // and
>
> if ( !inverse )
> return line_matched;
> else
> return !line_matched;
>
>

() means if:
if there are different the result is false else true

so in one line :
(inverse ^ line_matched) ? return false : return true;

I think that the two blocks aren't logically equivalent .

K

Kristoffel, Apr 5, 2004

3. ### KristoffelGuest

Buck Turgidson wrote:
> Can someone check me on if the following two blocks are logically
> equivalent? Is there an even better way to write this with a one-liner?
>
>
>
> return (inverse ^ line_matched);
>
>
> // and
>
> if ( !inverse )
> return line_matched;
> else
> return !line_matched;
>
>

(inverse ^ line_matched) means if:
if there are different the result is false else true

so in one line :
(inverse ^ line_matched) ? return false : return true;

I think that the two blocks aren't logically equivalent .

K

Kristoffel, Apr 5, 2004
4. ### =?ISO-8859-1?Q?Daniel_Sj=F6blom?=Guest

Buck Turgidson wrote:
> Can someone check me on if the following two blocks are logically
> equivalent?

Sure. Write some truth tables.

Is there an even better way to write this with a one-liner?
>
>
>
> return (inverse ^ line_matched);

inv ^ line ->

line=1 line=0
inv=1 0 1
inv=0 1 0

> // and
>
> if ( !inverse )
> return line_matched;
> else
> return !line_matched;

!inv ? line : !line ->

line=1 line=0
inv=1 0 1
inv=0 1 0

So yes, they are equivalent if I remember my logic right. There is
probably nothing better. An XOR is one of the cheapest operations on
most processors.
--
Daniel Sjöblom
Remove _NOSPAM to reply by mail

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=, Apr 5, 2004
5. ### Chris RiesbeckGuest

In article <ymgcc.61302\$-ops.be>,
Kristoffel <> wrote:

> (inverse ^ line_matched) means if:
> if there are different the result is false else true

just the opposite -- X ^ Y is true if X and Y are different,
i.e., exactly 1 is true

> so in one line :
> (inverse ^ line_matched) ? return false : return true;

anything ? return false : return true;

can always be written more briefly

return !anything;

Chris Riesbeck, Apr 5, 2004
6. ### Chris RiesbeckGuest

In article <>,
"Buck Turgidson" <> wrote:

> Can someone check me on if the following two blocks are logically
> equivalent?

==

Chris Riesbeck, Apr 5, 2004
7. ### Joona I PalasteGuest

Chris Riesbeck <> scribbled the following:
> In article <ymgcc.61302\$-ops.be>,
> Kristoffel <> wrote:
>> (inverse ^ line_matched) means if:
>> if there are different the result is false else true

> just the opposite -- X ^ Y is true if X and Y are different,
> i.e., exactly 1 is true

>> so in one line :
>> (inverse ^ line_matched) ? return false : return true;

> anything ? return false : return true;

> can always be written more briefly

> return !anything;

And that way it'll even compile and everything. The operands of the ?:
operator have to be expressions, and return is not an expression.

--
/-- Joona Palaste () ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I said 'play as you've never played before', not 'play as IF you've never
played before'!"
- Andy Capp

Joona I Palaste, Apr 5, 2004
8. ### Roedy GreenGuest

On Mon, 05 Apr 2004 16:19:14 GMT, "Buck Turgidson" <>
wrote or quoted :

> return (inverse ^ line_matched);

Most programmers are terrified of xor. You can write that in a way
more familiar to the peanut gallery with:

return inverse != line_matched;

presuming both are booleans, not ints.

--
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Roedy Green, Apr 5, 2004
9. ### Roedy GreenGuest

On Mon, 05 Apr 2004 17:05:46 GMT, Kristoffel <> wrote
or quoted :

>so in one line :
> (inverse ^ line_matched) ? return false : return true;

Why bother with ?. You already have a boolean.

--
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Roedy Green, Apr 5, 2004
10. ### KristoffelGuest

Chris Riesbeck wrote:
> In article <ymgcc.61302\$-ops.be>,
> Kristoffel <> wrote:
>
>
>>(inverse ^ line_matched) means if:
>> if there are different the result is false else true

>
>
> just the opposite -- X ^ Y is true if X and Y are different,
> i.e., exactly 1 is true

rigth, I was wrong.

Kristoffel, Apr 5, 2004