XOR Boolean

B

Buck Turgidson

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;
 
K

Kristoffel

Buck said:
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
 
K

Kristoffel

Buck said:
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?=

Buck said:
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.
 
C

Chris Riesbeck

Kristoffel said:
(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;
 
J

Joona I Palaste

just the opposite -- X ^ Y is true if X and Y are different,
i.e., exactly 1 is 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 ([email protected]) ------------- 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
 
R

Roedy Green

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top