473,386 Members | 1,799 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,386 software developers and data experts.

How does this swap() work?

Hi ALL,
I don't know why the function bellow can change the pointer of the two value
*x *y?
void swap(int *x,int *y)
{
*x = *x ^ *y;
*y = *x ^ *y;
*x = *x ^ *y;
}
Jul 13 '07 #1
7 1755
xuyao wrote:
Hi ALL,
I don't know why the function bellow can change the pointer of the two value
*x *y?
void swap(int *x,int *y)
{
*x = *x ^ *y;
*y = *x ^ *y;
*x = *x ^ *y;
}
Break open your C textbook and look-up what the bitwise exclusive OR
operator does. Writing out the operations on paper will help you to
understand.

Jul 13 '07 #2
On 7 13 , 4 43 , santosh <santosh....@gmail.comwrote:
xuyao wrote:
Hi ALL,
I don't know why the function bellow can change the pointer of the two value
*x *y?
void swap(int *x,int *y)
{
*x = *x ^ *y;
*y = *x ^ *y;
*x = *x ^ *y;
}

Break open your C textbook and look-up what the bitwise exclusive OR
operator does. Writing out the operations on paper will help you to
understand.
I konw now ,i wonder why they use that kind of swap(),is that swap()
above faster than this function bellow or the other reasons

void fun(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}

Jul 13 '07 #3
xuyao <xu******@gmail.comwrites:
>I konw now ,i wonder why they use that kind of swap(),is that swap()
above faster than this function bellow or the other reasons
>void fun(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}

I don't believe that anyone with an eye for understandable, clear,
portable code uses that XOR technique.

It's often provided as a (poor) learning exercise for undergraduate students
and, once understood, should be dispatched to the trashcan of geeky tricks.

--
Chris.
Jul 13 '07 #4
Chris McDonald wrote:
xuyao <xu******@gmail.comwrites:
>>I konw now ,i wonder why they use that kind of swap(),is that swap()
above faster than this function bellow or the other reasons
>>void fun(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}


I don't believe that anyone with an eye for understandable, clear,
portable code uses that XOR technique.

It's often provided as a (poor) learning exercise for undergraduate students
and, once understood, should be dispatched to the trashcan of geeky tricks.
Especially since it [the XOR-* hack] is broken in the degenerate case.

--
RIP Donald Michie 11 November 1923 - 7 July 2007

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Jul 13 '07 #5
xuyao <xu******@gmail.comwrote:
On 7 13 , 4 43 , santosh <santosh....@gmail.comwrote:
xuyao wrote:
I don't know why the function bellow can change the pointer of the two value
*x *y?
void swap(int *x,int *y)
{
*x = *x ^ *y;
*y = *x ^ *y;
*x = *x ^ *y;
}
Break open your C textbook and look-up what the bitwise exclusive OR
operator does. Writing out the operations on paper will help you to
understand.

I konw now ,i wonder why they use that kind of swap(),is that swap()
above faster than this function bellow or the other reasons

void fun(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
Quite the contrary. Since the version with the temporary variable is
more logical, more expressive, and corresponds better to what really is
supposed to happen, it is more likely to be optimised well than the XOR
hack. Therefore, this code is better in all counts, and the first code
should be relegated to the scrap-books of schoolboy hacks.

Richard
Jul 13 '07 #6
On Fri, 13 Jul 2007 16:09:35 +0800, xuyao wrote:
Hi ALL,
I don't know why the function bellow can change the pointer of the two value
*x *y?
....as long as x doesn't equal y...
>
void swap(int *x,int *y)
{
*x = *x ^ *y;
*y = *x ^ *y;
*x = *x ^ *y;
}
--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Jul 13 '07 #7
"xuyao" writes:
On 7 13 , 4 43 , santosh <santosh....@gmail.comwrote:
>xuyao wrote:
Hi ALL,
I don't know why the function bellow can change the pointer of the two
value
*x *y?
void swap(int *x,int *y)
{
*x = *x ^ *y;
*y = *x ^ *y;
*x = *x ^ *y;
}

Break open your C textbook and look-up what the bitwise exclusive OR
operator does. Writing out the operations on paper will help you to
understand.

I konw now ,i wonder why they use that kind of swap(),is that swap()
above faster than this function bellow or the other reasons
Because they want to show the world how incredibly bright they are. Their
mommy didn't praise them enough when she potty-trained them.
Jul 13 '07 #8

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

Similar topics

8
by: Paul | last post by:
This method: static void swap(int a, int b) { int c = a; a = b; b = c; } does not swap the values of a and b over. It doesn't work for String variables either. However the following method...
50
by: Steve | last post by:
How do you rewrite the swap function without using a tmp variable in the swap function???? int main() { int x = 3; int y = 5; // Passing by reference
3
by: Dietmar Prantner | last post by:
Hello, I tried a Javascript on my homepage that works fine on Netscape, works nearlyx normal on Opera but does not work at all on Internet Explorer. Is there anybody out there who can help? I...
2
by: ma740988 | last post by:
So I'm reading the C++ coding standards(Shutter & Andrei), more specifically item 56. There's a statement: "Prefer to provide a nonmember swap function in the same namespace as your type when...
74
by: Suyog_Linux | last post by:
I wish to know how the free()function knows how much memory to be freed as we only give pointer to allocated memory as an argument to free(). Does system use an internal variable to store allocated...
9
by: Jongmin Lee | last post by:
Hi Everybody, I have very simple code snippet to explain my problem. Class "Swap" is construncted in "Main" with two initial variables. Later, "Swap" class is going to swap those two...
9
by: Andy B | last post by:
If I bought one of these boxes/OS combos as a postgresql database server, would postgresql be able to make the best use of it with a huge (e.g. 40GB) database? Box: HP ProLiant DL585, with ...
2
by: webdeveloper | last post by:
sorry about that, This is the event handling element: <div id="menu"> <ul> <li>.... <li><a href="javascript:expand_menu(document.getElementById('extend'))" id="extend">Text <img src=""...
28
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.