XOR Boolean

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

  1. 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
    #1
    1. Advertising

  2. Buck Turgidson

    Kristoffel Guest

    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
    #2
    1. Advertising

  3. Buck Turgidson

    Kristoffel Guest

    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
    #3
  4. 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
    #4
  5. 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
    #5
  6. In article <>,
    "Buck Turgidson" <> wrote:

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


    ==
    Chris Riesbeck, Apr 5, 2004
    #6
  7. 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
    #7
  8. Buck Turgidson

    Roedy Green Guest

    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.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Apr 5, 2004
    #8
  9. Buck Turgidson

    Roedy Green Guest

    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.


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Apr 5, 2004
    #9
  10. Buck Turgidson

    Kristoffel Guest

    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
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cryptogirl

    How can you xor ArrayLists?

    cryptogirl, Feb 24, 2006, in forum: Java
    Replies:
    7
    Views:
    806
  2. evan
    Replies:
    1
    Views:
    1,092
    John Harrison
    Jun 28, 2003
  3. J Leonard
    Replies:
    4
    Views:
    12,631
    Mark Space
    Jan 19, 2008
  4. Dr. Phillip M. Feldman

    missing 'xor' Boolean operator

    Dr. Phillip M. Feldman, Jul 14, 2009, in forum: Python
    Replies:
    71
    Views:
    2,122
    Terry Reedy
    Jul 27, 2009
  5. Metre Meter
    Replies:
    7
    Views:
    358
    Metre Meter
    Aug 6, 2010
Loading...

Share This Page