Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > XOR Boolean

Reply
Thread Tools

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;


 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
=?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
 
Reply With Quote
 
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;
 
Reply With Quote
 
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?


==
 
Reply With Quote
 
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
 
Reply With Quote
 
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.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
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.


--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Subtle difference between boolean value and boolean comparison? Metre Meter Javascript 7 08-06-2010 08:40 PM
missing 'xor' Boolean operator Dr. Phillip M. Feldman Python 71 07-27-2009 06:05 AM
difference between 'boolean' and 'java.lang.Boolean' J Leonard Java 4 01-19-2008 02:56 AM
How can you xor ArrayLists? cryptogirl Java 7 02-26-2006 11:15 PM
Evaluate such an expression as: (A and B) xor (C or D) evan C++ 1 06-28-2003 06:36 AM



Advertisments