|
|
Subscribe / Log in / New account

C-Style Logical XOR

C-Style Logical XOR

Posted Jun 11, 2024 16:37 UTC (Tue) by dskoll (subscriber, #1630)
In reply to: C-Style Logical XOR by willy
Parent article: perl v5.40.0 released

Yup, xor is not often used in general-purpose software, though it is used in quite a few encryption and hashing algorithms. But that's bitwise-xor. I'm scratching my head trying to come up with a scenario where logical-xor is needed.

I agree that reserving ^ for exponentiation would have been better than reserving it for xor, but that's water under the bridge.


to post comments

C-Style Logical XOR

Posted Jun 11, 2024 17:52 UTC (Tue) by NYKevin (subscriber, #129325) [Link]

I think I have occasionally wanted logical XNOR in general programming (I don't write much/any Perl), but I usually just end up writing bool(x) == bool(y) (replacing bool() with whatever the language's coerce-to-booleanish syntax looks like, or omitting it if we're in a strongly-typed language that has first-class bools). IMHO that's easier to read than a hypothetical XOR operator would be anyway. I suppose it might be useful in bash-like languages, where you'd have to screw around with $? and temporary variables to write an XOR/XNOR using equality. But no bash-like language actually has this feature (that I know of).

C-Style Logical XOR

Posted Jun 11, 2024 23:04 UTC (Tue) by MarcB (subscriber, #101804) [Link]

I recall using it once, for configuration validation where options were mutually exclusive. But it really is rare.

C-Style Logical XOR

Posted Jun 14, 2024 15:57 UTC (Fri) by magnus (subscriber, #34778) [Link] (1 responses)

If you're writing code to draw a chess board and deciding which color to use in each square, logical xor could be handy (row number is even xor column number is even). But thats a bit of a niche case :)

C-Style Logical XOR

Posted Jun 14, 2024 16:48 UTC (Fri) by atnot (subscriber, #124910) [Link]

Surely in that case you'd usually rather write something such as `col ^ row & 1`? It's kind of a bit too smart, but not much more than using an xor operator.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds