473,890 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why isn't there a logical XOR operator?

Of course one can get the effect with appropriate use of existing
operators, but a ^^ operator would make for nice symmetry (as well as
useful to me in something I'm working on). Am I the only one who
would find it useful?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05
80 35192
On 3 Feb 2004 16:09:18 GMT, Da*****@cern.ch (Dan Pop) wrote:
Are you still convinced that it would be useful?


Yes, for the same reason as || and &&, albeit there's no shortcutting. It
makes it *obvious* that a purely boolean expression is involved. !a ^ !b
isn't obviously purely boolean; the central operator is not boolean.
--
#include <standard.discl aimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list
Nov 14 '05 #21
Em Tue, 03 Feb 2004 17:05:14 +0000, CBFalconer escreveu:
Christopher Benson-Manica wrote: <snip>
if (!!a ^ !!b) ....

c = !!a ^ !!b;


Why !!a ? Doesn't this have the same effect of a? Like
if (a ^ b) ....
c = a ^ b;

Could you enlighten us?

--
Quidquid latine dictum sit altum viditur

Nov 14 '05 #22

On Tue, 3 Feb 2004, Dan Pop wrote:

Christopher Benson-Manica writes: [re: introducing a logical XOR operator]
Of course one can get the effect with appropriate use of existing
operators, but a ^^ operator would make for nice symmetry (as well as
useful to me in something I'm working on).


Because ^ is good enough, given the fact that a logical XOR operator
couldn't have any shortcircuiting semantics. !a ^ !b yields exactly
the same result as the hypothetical ^^ operator.


Possibly dumb, and possibly OT, question: What if 'a' and/or 'b' are
doubles with the IEEE value NaN, that screws up comparisons? Does
the !a^!b <==> a^^b equivalence still hold? (Or do NaN semantics already
make nonsense of certain operations?)
If a and b are already
logical expressions (i.e. expressions that evaluate to 0 or 1), a ^ b is
enough.
Am I the only one who would find it useful?


Are you still convinced that it would be useful?


Well, a logical xor operator wouldn't add anything to the capabilities
of the language, but then neither does having a special operator for
subtraction. I have, on occasion, run across situations where an XOR
operator would have been useful. Christopher mentioned command-line
argument parsing, such as:

[...]
switch (argv[i][j]) {
case 'K': KNRStyle = 1; break;
case 'A': AnsiStyle = 1; break;
}
[...]
if (KNRStyle ^^ AnsiStyle)
process(infp, outfp);
else
do_error("You must specify exactly one of -K, -A!\n");

Here the ^^ operator would capture the spirit of the condition better
than the != alternative, as well as being slightly more robust. The
suggested alternative,

if (!KNRStyle != !AnsiStyle)

is IMO so obfuscated as to be actually worse than simply enumerating
the cases:

if (KNRStyle && !AnsiStyle || AnsiStyle && !KNRStyle)
I also submit for consideration, as I've done before, the usefulness
of logical operators when dealing with pointers:

struct btnode {
int value;
struct btnode *left;
struct btnode *right;
};

int bt_IsUselessBra nch(struct btnode *p)
{
return (p->value == 0) && (p->left ^^ p->right);
}

versus

return (p->value == 0) && (!(p->left) != !(p->right));

Of course, both cases are pretty ugly, here. But I haven't yet
come up with a good example. ;-)
And while I think it would have been cool if C had been designed
with a ^^ operator from the start, IMHO it's much too late now to
try adding it to the standard language. C doesn't need any more
syntax, and even if it did, I don't think the new syntax would find
many willing users. A new version of C would IMHO suffer the same
problems as C99 still suffers w.r.t. market share, only worse.
Now, a *new* language... :-)

-Arthur
Nov 14 '05 #23
José de Paula <jo***********@ ig.com.br> scribbled the following:
Em Tue, 03 Feb 2004 17:05:14 +0000, CBFalconer escreveu:
Christopher Benson-Manica wrote: <snip>
if (!!a ^ !!b) ....

c = !!a ^ !!b;

Why !!a ? Doesn't this have the same effect of a? Like
if (a ^ b) ....
c = a ^ b; Could you enlighten us?


No, it's not the same effect. The outcome is mostly the same, but
there are differences.
Suppose a is 1 and b is 2. a ^ b will now be 3, which is different
from 0, and thus C considers it true.
However !!a will be 1, and !!b will also be 1, so !!a ^ !!b will be
0, and thus C considers it false.
The ! operator is defined as returning 1 if its operand is 0, or
returning 0 if its operand is anything other. !! is two ! operators
together, they return 0 if their operand is 0, or 1 if their operand
is anything other.
This is often used as a neat trick to "force" non-zero values to 1,
making sure bitwise operators work right in place of logical operators.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Make money fast! Don't feed it!"
- Anon
Nov 14 '05 #24

On Tue, 3 Feb 2004, [iso-8859-1] José de Paula wrote:

Em Tue, 03 Feb 2004 17:05:14 +0000, CBFalconer escreveu:
Christopher Benson-Manica wrote:

if (!!a ^ !!b) ....

c = !!a ^ !!b;


Why !!a ? Doesn't this have the same effect of a? Like
if (a ^ b) ....
c = a ^ b;

Could you enlighten us?


What is the value of 42?
What is the value of !!42?
What is the value of 42 ^ 6?
What is the value of !!42 ^ !!6?

-Arthur,
rhetorically
Nov 14 '05 #25
José de Paula wrote:
Em Tue, 03 Feb 2004 17:05:14 +0000, CBFalconer escreveu:

Christopher Benson-Manica wrote:


<snip>
if (!!a ^ !!b) ....

c = !!a ^ !!b;

Why !!a ? Doesn't this have the same effect of a? Like
if (a ^ b) ....
c = a ^ b;

Could you enlighten us?

Assume
a = 2;
b = 7;

Then
a ^ b == 2 ^ 7
== 5

which evaluates false in an if() statement

However
!a == 0

!!a == ! (!a)
== 1

!b == 0

!!b == ! (!b)
== 1

!!a ^ !!b == 1 ^ 1
== 0

which evaluates true in an if() statement
--
Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)

Nov 14 '05 #26
Gak!! Another fingercheck/brainfart

Lew Pitcher wrote:
José de Paula wrote:
Em Tue, 03 Feb 2004 17:05:14 +0000, CBFalconer escreveu:

Christopher Benson-Manica wrote:

<snip>
if (!!a ^ !!b) ....

c = !!a ^ !!b;


Why !!a ? Doesn't this have the same effect of a? Like
if (a ^ b) ....
c = a ^ b;

Could you enlighten us?


Assume
a = 2;
b = 7;

Then
a ^ b == 2 ^ 7
== 5

which evaluates false in an if() statement

which evaluates /true/ in an if() statement

However
!a == 0

!!a == ! (!a)
== 1

!b == 0

!!b == ! (!b)
== 1

!!a ^ !!b == 1 ^ 1
== 0

which evaluates true in an if() statement

which evaluates /false/ in an if() statement

--
Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)

Nov 14 '05 #27
Arthur J. O'Dwyer wrote:

On Tue, 3 Feb 2004, Dan Pop wrote:

Christopher Benson-Manica writes:

[re: introducing a logical XOR operator]
Of course one can get the effect with appropriate use of existing
operators, but a ^^ operator would make for nice symmetry (as well as
useful to me in something I'm working on).


Because ^ is good enough, given the fact that a logical XOR operator
couldn't have any shortcircuiting semantics. !a ^ !b yields exactly
the same result as the hypothetical ^^ operator.


Possibly dumb, and possibly OT, question: What if 'a' and/or 'b' are
doubles with the IEEE value NaN, that screws up comparisons? Does
the !a^!b <==> a^^b equivalence still hold? (Or do NaN semantics already
make nonsense of certain operations?)
If a and b are already
logical expressions (i.e. expressions that evaluate to 0 or 1), a ^ b is
enough.
Am I the only one who would find it useful?


Are you still convinced that it would be useful?


Well, a logical xor operator wouldn't add anything to the capabilities
of the language, but then neither does having a special operator for
subtraction. I have, on occasion, run across situations where an XOR
operator would have been useful. Christopher mentioned command-line
argument parsing, such as:

[...]
switch (argv[i][j]) {
case 'K': KNRStyle = 1; break;
case 'A': AnsiStyle = 1; break;
}
[...]
if (KNRStyle ^^ AnsiStyle)
process(infp, outfp);
else
do_error("You must specify exactly one of -K, -A!\n");

Here the ^^ operator would capture the spirit of the condition better
than the != alternative, as well as being slightly more robust. The
suggested alternative,

if (!KNRStyle != !AnsiStyle)

is IMO so obfuscated as to be actually worse than simply enumerating
the cases:

if (KNRStyle && !AnsiStyle || AnsiStyle && !KNRStyle)

I also submit for consideration, as I've done before, the usefulness
of logical operators when dealing with pointers:

struct btnode {
int value;
struct btnode *left;
struct btnode *right;
};

int bt_IsUselessBra nch(struct btnode *p)
{
return (p->value == 0) && (p->left ^^ p->right);
}

versus

return (p->value == 0) && (!(p->left) != !(p->right));

Of course, both cases are pretty ugly, here.


.... without the extra parentheses:
return (p->value == 0) && (!p->left != !p->right);

explicit comparisons ...
((p->left == NULL) != (p->right == NULL));

With explicit comparisons against NULL,
as long as you have an odd number of nots, it's all the same.

((p->left == NULL) != (p->right == NULL));
((p->left != NULL) != (p->right != NULL));
((p->left != NULL) == (p->right == NULL));
--
pete
Nov 14 '05 #28
Em Tue, 03 Feb 2004 21:36:42 +0000, Joona I Palaste escreveu:
<snip>


I'm enlightened, thanks!

--
Quidquid latine dictum sit altum viditur
José - Threading The Long Path to Nerdvana

Nov 14 '05 #29
Christopher Benson-Manica <at***@nospam.c yberspace.org> wrote:
# Of course one can get the effect with appropriate use of existing
# operators, but a ^^ operator would make for nice symmetry (as well as
# useful to me in something I'm working on). Am I the only one who
# would find it useful?

#define logical_equiv(p ,q) (!(p)==!(q))
#define logical_xor(p,q ) (!(p)!=!(q))
#define logical_implies (p,q) (!(p)<=!(q))

--
Derk Gwen http://derkgwen.250free.com/html/index.html
I have no respect for people with no shopping agenda.
Nov 14 '05 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
14422
by: user | last post by:
Hi, Is it possible to override assignment, the way that '+' can be overridden for example? Thanks, Toby
14
2348
by: lutorm | last post by:
Hi everyone, I'm trying to use istream_iterators to read a file consisting of pairs of numbers. To do this, I wrote the following: #include <fstream> #include <vector> #include <iterator> using namespace std;
5
1381
by: kenny Nguyen | last post by:
Hi, Does anyone know the operator "=+"? If you do, what special method is related to this operator? Is it the __iadd__ method, but I think this is related to "+=" operator. Thanks, Kenny
67
8686
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used in low-level numerical computations, replacing Fortran in newer projects. Check, for example, sourceforge.net or freshmeat.net But neither language offers a primitive exp operator.
12
2228
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid overloading of == and != but instead translate each call of objA==objB automatically in System.Object.Equals(objA, objB). This would remove inconsistencies like myString1==myString2
33
1543
by: SpreadTooThin | last post by:
Can these operators be overloaded? If so. How?
18
3242
by: Ranganath | last post by:
Why is throw keyword considered as an operator?
6
11865
by: Rudi | last post by:
Hello! I'm searching like a similar syntax for if(): int i=5; if (i in ) doSomething; e.g. enum TaskStates {Idle,Proc1,Proc2, ... ProcN}
19
3540
by: C++Liliput | last post by:
I have a custom String class that contains an embedded char* member. The copy constructor, assignment operator etc. are all correctly defined. I need to create a map of my string (say a class called MyString) and an integer i.e. std::map<MyString, int>. Whenever I insert the elements in the map using the subscript operator, I noticed that the copy constructor for MyString is invoked more number of times than if I do it using the insert()...
0
11207
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10443
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9612
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7153
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5830
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4652
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4251
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.