Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Logical XOR

Reply
Thread Tools

Logical XOR

 
 
Martin Wells
Guest
Posts: n/a
 
      09-21-2007


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

 
Reply With Quote
 
 
 
 
Martin Wells
Guest
Posts: n/a
 
      09-21-2007

> !!a != !!b



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

!a != !b

Martin

 
Reply With Quote
 
 
 
 
fred.l.kleinschmidt@boeing.com
Guest
Posts: n/a
 
      09-21-2007
On Sep 21, 11:30 am, Martin Wells <(E-Mail Removed)> wrote:
> > !!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

 
Reply With Quote
 
Martin Wells
Guest
Posts: n/a
 
      09-21-2007

> 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

 
Reply With Quote
 
Michal Nazarewicz
Guest
Posts: n/a
 
      09-21-2007
> On Sep 21, 11:30 am, Martin Wells <(E-Mail Removed)> wrote:
>> > !!a != !!b

>>
>> About three seconds after I sent that post I realised I cuda written:
>>
>> !a != !b


http://www.velocityreviews.com/forums/(E-Mail Removed) 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--
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-21-2007
(E-Mail Removed) writes:
> On Sep 21, 11:30 am, Martin Wells <(E-Mail Removed)> wrote:
>> > !!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) (E-Mail Removed) <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"
 
Reply With Quote
 
Tor Rustad
Guest
Posts: n/a
 
      09-21-2007
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>
 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      09-21-2007
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

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-21-2007
Keith Thompson wrote:
> (E-Mail Removed) 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

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      09-21-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Logical XOR of strings Michael W. Ryder Ruby 9 05-31-2006 07:20 AM
How can you xor ArrayLists? cryptogirl Java 7 02-26-2006 11:15 PM
XOR Boolean Buck Turgidson Java 9 04-05-2004 09:52 PM
Why isn't there a logical XOR operator? Christopher Benson-Manica C Programming 80 02-06-2004 01:12 AM
Evaluate such an expression as: (A and B) xor (C or D) evan C++ 1 06-28-2003 06:36 AM



Advertisments