1754793 Members
3945 Online
108825 Solutions
New Discussion юеВ

n1 ^= n1? What is this?

 
SOLVED
Go to solution
Mary Rice
Frequent Advisor

n1 ^= n1? What is this?

Hello Experts,

I thought I knew something about C until I was given the job of making some minor changes to some very old (K & R , grrrr) C.

This code is filled with statements like this:
n1 ^= n1;
n2 ^= n2;

What is this doing. I took one out and the program instantly started core dumping. BTW, n1 and n2 are ints.

Please help quickly, Mary
7 REPLIES 7
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: n1 ^= n1? What is this?

Hi Mary:

As soon as I saw this, I chuckled. Would you believe that this could have been written as:
n1 = n2 = 0;

The original programmer must have been an assembly programmer at one time because XOR'ing a value with itself is an old assembly idiom to quickly zero a value (ususally a register). I'm even willing to bet that n1 and n2 were declared as register int's.

Regards, Clay
If it ain't broke, I can fix that.
harry d brown jr
Honored Contributor

Re: n1 ^= n1? What is this?

damn bit twidlers, oh wait, I'm one of those, ouch...

live free or die
harry
Live Free or Die
Martin Johnson
Honored Contributor

Re: n1 ^= n1? What is this?

Clay,

Your age is showing! :-)

Haven't seen ^= in years. += is a little more common though, but I haven't that lately either.

Marty < += increment by one>
Rodney Hills
Honored Contributor

Re: n1 ^= n1? What is this?

Actually Marty, += is increment by value to the right of =. ++ is increment by one.

Ever since perl, I haven't had to do any C programming. (and I'm glad of it!)

;-)

-- Rod Hills
There be dragons...
Martin Johnson
Honored Contributor

Re: n1 ^= n1? What is this?

You're right Rodney. I haven't done any serious C programming in 12 years. I no longer have a C manual at my desk. Had to rely on 12 year old memories (bad move!)

Thanks for the clarification.
Marty
Mary Rice
Frequent Advisor

Re: n1 ^= n1? What is this?

Yep Clay, you nailed it. I put in some printf statements and that is exactly what is happening. I would have never figured that out. BTW, those were register ints.

Now I have another question. One of the functions does this:

n1 ^= n2;
n2 ^= n1;
n1 ^= n2;

Neither n1 nor n2 are zeroed so what is this crazy statement doing?

I may have some more questions. I'll assign points then.

Thank You,
Mary
A. Clay Stephenson
Acclaimed Contributor

Re: n1 ^= n1? What is this?

I love this guy! Mary, where were you when they taught XOR's? Believe it or not, those 3 statements exchange the values of n1 and n2 without a temporary value.

An equivalent would be:
temp = n1;
n1 = n2;
n2 = temp;

I KNOW that this guy was an Assembly programmer now. His trick, would only work on wordsize or less variables.

Let's see if I can show you how this works; remember and XOR compares two values and a bit is 1 only if exactly 1 of the same bit in both
values is 1.

These are binary values:
Initial. n1 n2
........ 110 101
n1 ^= n2 011 101
n2 ^= n1 011 110
n1 ^= n2 101 110

Note that n1 and n2 have now been swapped. If you put printf()'s before and after your C code, you should see the same. My psychic, Miss Cleo, tells me that this stuff was in some sort of sort function.

Regards, Clay

If it ain't broke, I can fix that.