473,385 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Logical XOR



Just wondering how you all go about performing a logical XOR in C. At
the moment I'm doing:

!!a != !!b

, which is quicker to write than:

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

If we were to write it as a macro, how would we go about seeking the
shortest execution time?

Martin

Sep 21 '07 #1
14 14017
!!a != !!b

About three seconds after I sent that post I realised I cuda written:

!a != !b

Martin

Sep 21 '07 #2
On Sep 21, 11:30 am, Martin Wells <war...@eircom.netwrote:
!!a != !!b

About three seconds after I sent that post I realised I cuda written:

!a != !b

Martin
If a and b are always boolean values (0 or 1), just use
a^b
--
Fred

Sep 21 '07 #3
If a and b are always boolean values (0 or 1), just use
a^b
NO WAY, YOU'RE SUCH A GENIUS.

If only I wanted a bitwise XOR, I'd be sorted.

Martin

Sep 21 '07 #4
On Sep 21, 11:30 am, Martin Wells <war...@eircom.netwrote:
!!a != !!b

About three seconds after I sent that post I realised I cuda written:

!a != !b
fr*****************@boeing.com writes:
If a and b are always boolean values (0 or 1), just use
a^b
If they are not you can join those two techniques to form !a ^ !b which
is probably faster then !a != !b because no branching is used (at least
on some architectures).

But, I assume that if at least one of the values (say a) is boolean the
following will be even faster: ((unsigned)(((signed)a)-1)) & b. Not
sure if it's not implementation specific though. (If neither is boolean
replace a with !a). Disadvantage is that if b is not boolean this will
not produce a boolean value.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Sep 21 '07 #5
fr*****************@boeing.com writes:
On Sep 21, 11:30 am, Martin Wells <war...@eircom.netwrote:
!!a != !!b

About three seconds after I sent that post I realised I cuda written:

!a != !b

Martin

If a and b are always boolean values (0 or 1), just use
a^b
Boolean values aren't necessarily 0 or 1 (unless you're restricting
yourself to the C99-specific type _Bool or bool); any non-zero value
of any scalar type is treated as true.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 21 '07 #6
Martin Wells wrote:
>
Just wondering how you all go about performing a logical XOR in C. At
the moment I'm doing:

!!a != !!b

, which is quicker to write than:

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

If we were to write it as a macro, how would we go about seeking the
shortest execution time?
!a ^ !b

--
Tor <torust [at] online [dot] no>
Sep 21 '07 #7
On Fri, 21 Sep 2007 11:28:43 -0700, Martin Wells wrote:
>

Just wondering how you all go about performing a logical XOR in C. At
the moment I'm doing:

!!a != !!b

, which is quicker to write than:

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

If we were to write it as a macro, how would we go about seeking the
shortest execution time?
If the second one is written as a macro, you can't know how many
times a and b are evaluated unless you know their truth value
beforehand. Beware of side effects. Go for (!(a) ^ !(b)).
--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen

Sep 21 '07 #8
Keith Thompson wrote:
fr*****************@boeing.com writes:
.... snip ...
>>
If a and b are always boolean values (0 or 1), just use a^b

Boolean values aren't necessarily 0 or 1 (unless you're restricting
yourself to the C99-specific type _Bool or bool); any non-zero value
of any scalar type is treated as true.
If you are operating on !(expression) the values are always 0 or
1.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Sep 21 '07 #9
Martin Wells wrote:
>
!!a != !!b

About three seconds after I sent that post I realised I cuda written:

!a != !b
That's how I write it.
Take a look at the controling expression in the if statement:

unsigned char bit_rev(unsigned char byte)
{
unsigned hi_mask = ((unsigned char)-1 >1) + 1;
unsigned lo_mask = 1;

do {
if (!(byte & hi_mask) != !(byte & lo_mask)) {
byte ^= hi_mask | lo_mask;
}
hi_mask >>= 1;
lo_mask <<= 1;
} while (hi_mask lo_mask);
return byte;
}

--
pete
Sep 21 '07 #10
Army:
If the second one is written as a macro, you can't know how many
times a and b are evaluated unless you know their truth value
beforehand. Beware of side effects. Go for (!(a) ^ !(b)).

The problem of multiple evaluation can be solved by simply making the
macro name all uppercase.

Martin

Sep 21 '07 #11
Army:
>If the second one is written as a macro, you can't know how many
times a and b are evaluated unless you know their truth value
beforehand. Beware of side effects. Go for (!(a) ^ !(b)).
Martin Wells <wa****@eircom.netwrites:
The problem of multiple evaluation can be solved by simply making the
macro name all uppercase.
It doesn't solve the problem. It only gives a hint to programmer.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Sep 21 '07 #12
Michal:
Martin Wells <war...@eircom.netwrites:
The problem of multiple evaluation can be solved by simply making the
macro name all uppercase.

It doesn't solve the problem. It only gives a hint to programmer.

It explicitly indicates to the programmer that they're dealing with a
macro. Just like how we put a "THIS IS BLEACH" label on a container of
bleach.

Let them drink the bleach if they're really that incompetant.

Or, if you *really* wanna spoonfeed them, write a compiler that warns
when you pass a "side-effect expression" as an argument to a function
macro.

Martin

Sep 21 '07 #13

"Martin Wells" <wa****@eircom.netwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...
Michal:
>Martin Wells <war...@eircom.netwrites:
The problem of multiple evaluation can be solved by simply making the
macro name all uppercase.

It doesn't solve the problem. It only gives a hint to programmer.


It explicitly indicates to the programmer that they're dealing with a
macro. Just like how we put a "THIS IS BLEACH" label on a container of
bleach.

Let them drink the bleach if they're really that incompetant.

Or, if you *really* wanna spoonfeed them, write a compiler that warns
when you pass a "side-effect expression" as an argument to a function
macro.
It shouts.
The problem is that most time is spent reading code, not writing it, and the
shout is almost always a false warning. So people will mentally filter it
out.

It is like putting a red skull and crossbones on every single chemical in
the lab that might be slightly toxic if ingested. The really nasty stuff
then gets no emphasis.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Sep 22 '07 #14
On Fri, 21 Sep 2007 21:17:57 +0200, Michal Nazarewicz <mi****@tlen.pl>
wrote:
On Sep 21, 11:30 am, Martin Wells <war...@eircom.netwrote:
!!a != !!b

About three seconds after I sent that post I realised I cuda written:

!a != !b

fr*****************@boeing.com writes:
If a and b are always boolean values (0 or 1), just use
a^b

If they are not you can join those two techniques to form !a ^ !b which
is probably faster then !a != !b because no branching is used (at least
on some architectures).
I don't know of any architecture where ! doesn't need conditional
branching (the kind that usually matters here) but != does.
But, I assume that if at least one of the values (say a) is boolean the
following will be even faster: ((unsigned)(((signed)a)-1)) & b. Not
sure if it's not implementation specific though. (If neither is boolean
replace a with !a). Disadvantage is that if b is not boolean this will
not produce a boolean value.
Doesn't do XOR for a==1 b==0. You can do & but I'm pretty sure you
can't get nonboolean ^ to work because it's (algebraically) linear.

The casts aren't needed if either a or b is unsigned (and not stricly
narrower than int). unsigned_a - 1 wraps around safely. signed_a - 1
gives an in-range signed value which automatically converts to
unsigned when it meets unsigned_b across & . If both are signed using
a -1U is enough to force (everything) unsigned (unless strictly wider
than int, then you need -1UL or even -1ULL). Using the casts is
arguably clearer, but if any of your nonbooleans are (or may be) wider
than int you have to specify appropriately wide typenames.

I think the only way to do this for XOR is to fully smear the/each
nonboolean operand e.g.
aa = a; aa |= aa >1; aa |= aa >2; aa |= aa >4; etc.
bb = b similarly if necessary
aa ^ bb
You can shorten the dependency chain (which is limiting on some
(most?) modern architectures) by doing somewhat more computation:
aa = a; aa |= aa >1 | aa >2; aa |= aa >3 | aa >6; etc.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Oct 8 '07 #15

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

Similar topics

6
by: Hari Om | last post by:
Here are the details of my error log files: I execute the command and get following message at console: ---------------------------------------------------------------------- ../sqlldr...
7
by: Charles Crume | last post by:
Hello all; I have used dBASE, and other computer languages/databases, for years. They all have a logical field type. However, the version of MySQL used by the ISP hosting my site does not...
80
by: Christopher Benson-Manica | last post by:
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...
3
by: serge | last post by:
How do I determine which method I should use if I want to optimize the performance of a database. I took Northwind's database to run my example. My query is I want to retrieve the Employees'...
4
by: serge | last post by:
I am running a query in SQL 2000 SP4, Windows 2000 Server that is not being shared with any other users or any sql connections users. The db involves a lot of tables, JOINs, LEFT JOINs, UNIONS...
14
by: blueboy | last post by:
Hi, I am planning to automate a nighty restore of a DB on another server can someone point me in the right direction with the SQL script to modify the logical file names to the correct path and...
2
by: dbtwo | last post by:
Until today I always thought as long as you see a lot of logical reads as compared to physical reads, then you're good. But it looks like it isn't so. But doesn't logical read mean it's being read...
1
by: ags5406 | last post by:
Hi -- I posted this in a Fortran group but thought I'd post here as well. Any help is appreciated. I have a IVF10 DLL that is the calculation engine for a frontend application written in...
11
by: Dominic Vella | last post by:
I am using MS-Access2000. I can't seem to set the default values for Logical type fields. I start with Dim dbsTmp As Object ' I think it's DAO.Database Set dbsTmp =...
7
by: raylopez99 | last post by:
I have a logical drawing space much bigger than the viewport (the screen) and I'd like to center the viewport (the screen) to be at the center of the logical drawing space. After following the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.