Velocity Reviews > Java > XOR Boolean

# XOR Boolean

Buck Turgidson
Guest
Posts: n/a

 04-05-2004
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;

Kristoffel
Guest
Posts: n/a

 04-05-2004
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
Guest
Posts: n/a

 04-05-2004
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

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=
Guest
Posts: n/a

 04-05-2004
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

Chris Riesbeck
Guest
Posts: n/a

 04-05-2004
In article <ymgcc.61302\$(E-Mail Removed)-ops.be>,
Kristoffel <(E-Mail Removed)> 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
Guest
Posts: n/a

 04-05-2004
In article <(E-Mail Removed) m>,
"Buck Turgidson" <(E-Mail Removed)> wrote:

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

==

Joona I Palaste
Guest
Posts: n/a

 04-05-2004
Chris Riesbeck <(E-Mail Removed)> scribbled the following:
> In article <ymgcc.61302\$(E-Mail Removed)-ops.be>,
> Kristoffel <(E-Mail Removed)> 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 ((E-Mail Removed)) ------------- 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

Roedy Green
Guest
Posts: n/a

 04-05-2004
On Mon, 05 Apr 2004 16:19:14 GMT, "Buck Turgidson" <(E-Mail Removed)>
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
Guest
Posts: n/a

 04-05-2004
On Mon, 05 Apr 2004 17:05:46 GMT, Kristoffel <(E-Mail Removed)> 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.

Kristoffel
Guest
Posts: n/a

 04-05-2004
Chris Riesbeck wrote:
> In article <ymgcc.61302\$(E-Mail Removed)-ops.be>,
> Kristoffel <(E-Mail Removed)> 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.