Note that in portable C (that is, according to, say, the 1999 C standard), this only works for unsigned integral types. Obviously it won’t work for floating point or pointer or struct/union types. (Portably – if you don’t care about machine-specific code, you could convert pointers to integers on many platforms and it’ll just do what you want. But it’s not guaranteed to work everywhere.)
But also, under some (rare, but allowed) non-2s-complement signed integer representations, an xor might result in a “negative zero” that can be converted to a normal zero before continuing, resulting in the wrong sign bit setting on an output value…
Wow, thanks for the details Ken! Yeah, I imagine there are all sorts of nasty side effects when using a trick like this. Appreciate the background info.
[…] void swap (int& a, int& b) { a ^= b; // a = a XOR b b ^= a; // b = b XOR a a ^= b; // a = a XOR b } Why does it work? Here is one that explains it well: Swap two variables using XOR | BetterExplained I found another article on the same technique, along with two other techniques. However, I caution you to read the comments that follow it: 3 ways to swap variables without temp variable « Stream As the betterexplained.com article mentions, there is an XCHG instruction on some machines that allows you to swap two registers without using multiple XOR instructions. The important thing: be careful when using such unusual techniques. You never know how a program can be affected by a compiler, an assembler or libraries that are linked it. Only use it if absolutely necessary or if you merely wish to play with it in a small program. Back on-topic, those were examples of using bitwise operations to increase efficiency. Multiplication and division by 2 is fairly simple too when you only need to worry about integers. All that is required is knowledge of what operators that bit shifts use in your specific programming language, if the operation is supported in your programming language (Visual Basic does not support bit shifts natively). In C/C++ and Java, the operator is "<<" for a left shift (SHL/SAL in ASM) and ">>" for a right shift (SHR/SAR in ASM - which one is used can depend upon signedness of a value except in Java where all integers are signed integers). The bottom line: ASM is useful and the ASM ways that were used for efficiency are still quite useful at times. __________________ […]
When using pointers to the same location, when you do the first step (x = x xor y) => 0 you also set y to 0 as well since it’s at the same location. In this case, you’ve lost the value 1234 – x and y were sharing the same location and it was overwritten with 0.
Pretty much any compiler ever will substitute the standard swap for a single exchange instruction. This means the extra variable never gets made (unless it is used later on), and the swap is only one instruction. The triple xor is not recognized as a swap (and can’t be, since the result is different when they are equal), so it is slower in the end, as well as harder to understand.
can you please reconstruct my program…i am trying to retrieved the nth perfect number…this are my codes:
int no,you=6,div=1,they=0,her=0,as;
getch(); //the if statement will be executed if menu no. 3 is chosen
if(opt==3){
system(“color 33”);
cout>no;
while(her