Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!mcsun!ukc!strath-cs!rmacfarq From: rmacfarq@cs.strath.ac.uk (Roderick F MacFarquhar IE87) Newsgroups: comp.lang.c Subject: Re: Brain Teaser Summary: Exclusive OR Keywords: { void * x, * y; x <-> y; } Message-ID: <2817@baird.cs.strath.ac.uk> Date: 28 Mar 90 10:03:03 GMT References: <10289@wpi.wpi.edu> Reply-To: rmacfarq@cs.strath.ac.uk Organization: Comp. Sci. Dept., Strathclyde Univ., Scotland. Lines: 34 In article <10289@wpi.wpi.edu> oesterle@wpi.wpi.edu (Shawn H. Oesterle) writes: > >Problem: > Swap two pointers without using a third pointer. [code using temp deleted] >Hint: > Two numbers may be swaped without using any other memory by executing > the following statements: > > int x, y; /* or you can chose double, long, etc. */ > > x += y; > y = x - y; > x -= y; But isn't there is a problem of potential overflow on the x += y instruction. I think its been given before in this group but how about.. #define SWAP(X,Y) \ {(X) ^= (Y) ; \ (Y) ^= (X) ; \ (X) ^= (Y) ;} >Shawn Oesterle {oesterle@wpi.wpi.edu} _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ Roderick MacFarquhar - Information Eng IV, Strathclyde University Janet rmacfarq@cs.strath.ac.uk Internet rmacfarq%cs.strath.ac.uk@nsf.ac.uk Voice (but I'm rarely home - sob :'{ ) +44 (0)41 339 0263 'How many fibres are intertwined in a shreaded wheat biscuit ?' ~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~