• anon (unregistered)

    pretty standard, indeed.

  • (nodebb)

    call a xor function

    This makes my teeth itch, but if you change it like this, it doesn't:

    call an xor function

  • Jonathan (unregistered)

    I work in mostly C# and TypeScript and our team doesn't need to use bitwise operations often enough to know offhand what the operators are and their implications, so the XOR being there actually makes the code less readable at a glance to us.

    That being said, I can't think of a great way to write this condition without an XOR, so if I saw the XOR solution come up in a PR I would probably approve it.

  • some guy (unregistered)

    Like other commenters, I have yet to see the need for logical XOR, so I would fart in the general direction of the business requirements underpinning all those XOR alluded to in the post.

  • Tim (unregistered)

    You can simply use != on two booleans as an xor operator

    A project I worked on in C had a lot of functions where you had to pass in either A or B (both were strings), so this was easily validated by

    if (!!A != !!B) { }

  • Foo AKA Fooo (unregistered) in reply to Jonathan

    Given Booleans, inequality, i.e. bool(text) != bool(otherText), is the same as xor and might be more readable.

  • Foo AKA Fooo (unregistered)

    "But I also can't complain too much about this one. I hate it, don't get me wrong" Just shows how low we've sunken ...

  • Smithers (unregistered)

    What surprises me most here is that this was "all over the codebase." as soon as I saw the snippet I was thinking about how I'd do this in Python and... I'm note sure I've ever had to (and I write a lot of Python). I've branched across all three cases (and, xor, nor), but it's rare, if ever, that I've wanted to xor two conds without differentiating "both" from "neither." Makes me wonder what else the contractor is doing that makes this come up.

    And it's not like this is a Python thing either - C and all its syntactic descendants also have bitwise [and, or, xor, not] but only logical [and, or, not], also without causing any issue. Indeed the motivation for && and || seems to be not correct handling of values other than {0, 1}, but short-circuiting, which doesn't work for xor. And ! because ~1 is not 0 (Many years ago, I was brought up on (classic) VB and I do sometimes see the merit of its choice of -1 for truth.)

  • (nodebb)

    I wouldn't call it a WTF, but in my experience, XOR is rare enough to be flagged as a code smell. I don't remember ever having requirements that rely on either A or B, but not both. Except for code challenges like Advent of Code, but can be somewhat unusual.

  • Waverly Wizard (unregistered)

    I think I agree with other people, that it isn't a common thing. If it comes up from requirements (so not an algorithm thing), I think the best thing would actually be the long winded thing.

    If ((X || Y) && !(X && Y))

  • erichamion (unregistered) in reply to Steve_The_Cynic

    Either article will be wrong for some subset of readers because the correct article depends on one's pronunciation. Presumably you prefer "ex-or" rather than "zor"?

  • Industrial Automation Engineer (unregistered)

    I've been flipping bits for over 25 years now, and still this code hurts my eyes. Just write it out, please?!?

    The problem with XOR usually is that it is thoroughly misunderstood. (b1 XOR b2) XOR b3 will not result in an exclusive OR function (one, and only one of the bits is true)

  • Tinkle (unregistered)

    Anything that makes you stop and go Eh? is a WTF.

    At least re-write this as:

    if (cond1 or cond2) and !(cond1 and cond2): # do actual work

    Why is this is used in a lot of places?

  • (nodebb) in reply to Waverly Wizard

    Your suggestion of If ((X || Y) && !(X && Y)) requires more thinking upon looking than is necessary, but I like Tim's suggestion of if (!!A != !!B) { } even less, for the same reason. I would write if ((A && !B) || (B && !A)) { } because IMHO it is the most understandable to the most people.

  • (nodebb)

    Fscking bloody system. I'm logged in and my comment is still "held for moderation". A great start to my morning.

  • (nodebb)

    And xor is not not or or xor and or

  • (author) in reply to Steve_The_Cynic

    That makes sense if you pronounce it "ex or" and not "zor". It's a "zor" operation, when I say it.

  • (nodebb) in reply to Industrial Automation Engineer

    (b1 XOR b2) XOR b3 will not result in an exclusive OR function (one, and only one of the bits is true)

    Indeed. Once you extend XOR beyond just two inputs, it becomes a function to calculate the "even parity" parity bit of the inputs.

    (For the folks in the audience who've missed the point: if b1, b2 and b3 are all true, the function given above will be true as well, which is not the understanding of the programmer on the Clapham Omnibus(1) about "exclusive or".)

    (1) From the Britishism "the man on the Clapham Omnibus", meaning an average "everyman" type of figure. ("Clapham" == a borough in the southern part of Greater London, and "omnibus" == an obsolete term for "bus")

  • (nodebb) in reply to Remy Porter

    As you say, it depends on the pronunciation, but I pronounce it as "exor" (rather than "ex or"), so for me it's definitely a teeth-itcher.

    Addendum 2024-12-12 11:40: If I wanted a pronunciation that isn't "exor", these days I'd go for "ixor" or "ksor", i.e. French style. The French do not zedify initial X.

  • (nodebb) in reply to dpm

    Dunno about all that. I'd define an itty-bitty function intended to be tagged "inline" (or even "__always_inline" on compilers that support it) that calculates xor, so the if()s look like if ( xor( A, B ) ).

  • Scragar (unregistered) in reply to ggeens

    I see it a fair bit coming from business requirements where there's a lot of listing every possible option that could be employed. It's not uncommon to hear the four states coming from two bools represented as

    If X and Y then display combined option If X but not Y OR Y but not X then display individual option If neither X or Y then do not display any option

    This becomes a lot messier and more commin when you're working on a single system that's feature locked for 100+ different customers.

  • Brian Boorman (unregistered) in reply to Remy Porter

    As an EE, it is definitely not pronounced "zor". In software, XOR is an implementation of what originally was discrete logic "exclusive-or" where it's pronounced x-clusive, not z-clusive.

  • Rob (unregistered)

    The true wtf is the writer of this article not understanding that a boolean xor is the same as boolean unequal

  • (nodebb) in reply to Rob

    The true wtf is the writer of this article not understanding that a boolean xor is the same as boolean unequal

    Subject to the need to normalise truthy/falsy values into actual booleans first, but yes. Shame that K&R (or C89/90, C99, etc.) didn't include ^^ as a logical XOR in the same vein as && and ||.

  • SomeDBA (unregistered)

    Slow-clapping the opening paragraphs.

  • (nodebb) in reply to Remy Porter

    So you'd pronounce " Markov xor 0" as Mark of zorro" ?

  • PedanticRobot (unregistered)

    Maybe it's just me, but somehow getting "zor" from a shortening of "exclusive or" is a bigger WTF than anything in the article.

  • (nodebb)

    The only place I can think of for an XOR condition is where the "# do stuff" raises an exception.

  • (nodebb) in reply to dpm

    I would write if ((A && !B) || (B && !A)) { } because IMHO it is the most understandable to the most people.

    I like that one but also like

    !( (a && b) || (!a && !b) )
    

    as this will short circuit if both are true, and is still understandable (testing if a and b are not both true nor both false), and there can be some cases where such short circuiting can be desirable.

  • Randal L. Schwartz (github) in reply to Tim

    if (!!A != !!B) { }

    Which can be simplified one layer to if (!A != !B) { }. although I'm not sure that'd be any less confusing. :)

  • (nodebb)

    It can be ... If A & B have already been normalzed to Booleans from other truthy/falsey types of values. The !!A does normalization then no-op double-negation in very few characters. Once it's a common idion in your codebase it becomes pretty obvious.

  • (nodebb)

    if (A+B==1)

    or if you must worry about truthiness and also want to be clear that at integer addition, if ((int)(bool)A+(int)(bool)B == 1)

    Addendum 2024-12-12 17:24: Sorry for typoo

  • (nodebb)

    Programming is communication above all else. You, or one of your colleagues will come by your code in half a year, two years, or if you work in the same business as I do, a decade or two later. Will your code make your brain bleed?

    This code made my brain bleed.

    Not being one for code golf, let's just think this through. As I understand it, we want to check if one statement is true and only one, so:

    bool weHaveBeer = ...; bool weHaveGlasses = ...; if ((weHaveBeer && !weHaveGlasses) || (!weHaveBeer && weHaveGlasses)) runInCirclesScreamAndShout();

    You get what's happening here...

    If I misunderstood this... well, as I said, it's communication above all... so, then, very "well" communicated code....

    And yeah, I'm a c#/Java programmer and this discussion of "python xor not python" just made me an even more convinced such programmer... ;)

  • (author) in reply to Brian Boorman

    If it weren't for the fact that you're right, I'd argue for a "shor" operation as something that one might perform at the beach. Or not.

  • Naomi (unregistered) in reply to Rob

    The article pointed out why that isn't safe to rely on: Python is both dynamically typed and loosely typed, so there's no guarantee that the arguments are actually booleans.

    The idiomatic way to convert something to a boolean in Python is just bool(something), incidentally - no double bangs required.

  • Officer Johnny Holzkopf (unregistered) in reply to cellocgw

    Mark-off ksor zeerow.

  • (nodebb) in reply to Naomi

    No, Python is dynamically typed, but it's generally strictly typed. Other than some implicit conversions for numeric types, Python will raise an exception if you try to use a variable in a place that can't handle the variable's type.

  • Guessed (unregistered) in reply to Smithers

    C and all its syntactic descendants also have bitwise [and, or, xor, not] but only logical [and, or, not]

    Not completely true: Perl has logical xor (also known as ^^).

  • (nodebb) in reply to dpm

    Sorry, I disagree:

    (X && !Y) || (Y && !X) is correct, but I think is harder to parse the intention.

    ((X || Y) && !(X && Y)) is easier to read as "X or Y and [but] not both", which is the essence of XOR

  • Jill (unregistered)

    Another WTF is Python not having a boolean xor.

Leave a comment on “Ready Xor Not”

Log In or post as a guest

Replying to comment #:

« Return to Article