- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: n1 ^= n1? What is this?
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2002 11:20 AM
тАО07-19-2002 11:20 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2002 11:24 AM
тАО07-19-2002 11:24 AM
SolutionAs 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2002 11:38 AM
тАО07-19-2002 11:38 AM
Re: n1 ^= n1? What is this?
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2002 11:53 AM
тАО07-19-2002 11:53 AM
Re: n1 ^= n1? What is this?
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>
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2002 12:00 PM
тАО07-19-2002 12:00 PM
Re: n1 ^= n1? What is this?
Ever since perl, I haven't had to do any C programming. (and I'm glad of it!)
;-)
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2002 12:05 PM
тАО07-19-2002 12:05 PM
Re: n1 ^= n1? What is this?
Thanks for the clarification.
Marty
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2002 12:12 PM
тАО07-19-2002 12:12 PM
Re: n1 ^= n1? What is this?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-19-2002 12:22 PM
тАО07-19-2002 12:22 PM
Re: n1 ^= n1? What is this?
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