Swap Integers

This is a discussion on Swap Integers within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by CornedBee I suppose they decided to fix the mistake "clever" programmers are making by using this. As ...

  1. #31
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    Quote Originally Posted by CornedBee View Post
    I suppose they decided to fix the mistake "clever" programmers are making by using this. As for the machinery, it's nothing that isn't there to recognize far more complex patterns, such as loops that can/should be unrolled.
    It's not really a mistake if you know that the swapped objects are not the same. In a swap function, that is obviously an inappropriate assumption, but it isn't always.

    I wasn't really being serious, though.

  2. #32
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It's still a mistake, because there is not advantage to the method.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #33
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    Quote Originally Posted by CornedBee View Post
    It's still a mistake, because there is not advantage to the method.
    It requires no temporary variable.

  4. #34
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    > It requires no temporary variable.
    Your point being?
    Code:
    void foo ( int a, int b ) {
        a ^= b;
        b ^= a;
        a ^= b;
        printf( "%d %d\n", a, b );
    }
    
    void bar ( int a, int b ) {
        int temp = a;
        a = b;
        b = temp;
        printf( "%d %d\n", a, b );
    }
    By the time the optimiser (gcc) has finished with the second one, it has managed to simplify the code into printf("%d %d\n", b, a );
    The temp and the swap are GONE.
    Not so with the attempt to obfuscate the heck out of things in the mistaken belief that using a variable is the end of the world. As well as confusing the average reader, you also managed to hide the true intent of the code from the compiler as well.

    Clear simple code wins every time. If you insist on treating C like some high level assembler, then expect to get the code you write.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #35
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think this reasoning is not quite right. That's not what you need swapping for, where you really need values relocated.

    However, wouldn't xor be something more complicated as simple assignment? A quick test showed that using a temp is a bit faster.

  6. #36
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    Quote Originally Posted by Salem View Post
    The temp and the swap are GONE.
    If you have an optimizer...

    Not so with the attempt to obfuscate the heck out of things in the mistaken belief that using a variable is the end of the world.
    I certainly don't believe that. And anybody who claims to know CS should know this property. I never said it should be used in real code.

    Clear simple code wins every time. If you insist on treating C like some high level assembler, then expect to get the code you write.
    You're preaching to a choir... Why did I post it in the first place? I dunno, I was bored. Had I known you would jump all over me I wouldn't have bothered.

  7. #37
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    I'd never heard of this algorithm. The following talks about its efficiency vs. using a temporary.

    http://en.wikipedia.org/wiki/XOR_swap_algorithm

  8. #38
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by robatino View Post
    I'd never heard of this algorithm.
    Count yourself lucky.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #39
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    Quote Originally Posted by robatino View Post
    I'd never heard of this algorithm. The following talks about its efficiency vs. using a temporary.

    http://en.wikipedia.org/wiki/XOR_swap_algorithm
    I would just like to point out at this time, that any method which has it's own GOSH DANG WIKI PAGE cannot possibly be "obscure." This is something a CS person should know. I didn't say "use," I said "know."

    EDIT: Why should you know it? For one thing, it might be on your final in boolean algebra.

  10. #40
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    Quote Originally Posted by Dave_Sinkula View Post
    Count yourself lucky.
    Honestly... It's only obscure if you don't know it. How is this any different than shifting right by one in order to divide an integer by 2? I think it's something a person should be aware of, not necessarily that they should put it into practice in new code.

    If you understand it, then you can "fix" it when you see it in older code. Isn't that a good enough reason to be aware of it?

  11. #41
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    brewbuck: Your points are well understood by many of us here. We tend to be harsh on things that generally persuade young'uns to do the Wrong Thing, regardless of the intentions, merely -- hopefully -- to produce better programmers. There is no malice.

    There are a number of things that are easily beaten up merely by presentation:
    • void main()
    • fflush(stdin)
    • gets(string)
    • Etc.
    The XOR swap thing merely happens to be among them, but not (yet) in the FAQ.

    I don't know, I guess that it's just a 25-year-old solution to a 30-year-old problem that some compilers may possibly now fix up bad programming idioms that may be common.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #42
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by brewbuck View Post
    How is this any different than shifting right by one in order to divide an integer by 2?
    Doesn't this have issues depending on the implementation?

    (Seriously, it's been a long day and I'm forgetful.)

    [edit]And frankly, fixing a problem that does not exist is in itself a problem.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #43
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > This is something a CS person should know.
    I'm a mathematician. I don't have a formal CS background.

  14. #44
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    Quote Originally Posted by robatino View Post
    > This is something a CS person should know.
    I'm a mathematician. I don't have a formal CS background.
    I didn't mean you, personally. Sorry.

  15. #45
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    Doesn't this have issues depending on the implementation?
    Yes, since the C++ Standard leaves the exact result of a right shift as implementation defined for negative integers. For non-negative integers it is quite clear that a division by the nth power of two will be performed. So, (1 >> 1) == 0, but (-1 >> 1) == 0 or (-1 >> 1) == -1 are both possible, as are other interpretations of what happens on a right shift (e.g., largest possible positive integer value).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 3 of 5 FirstFirst 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  3. Integers into array.
    By livestrng in forum C Programming
    Replies: 10
    Last Post: 10-30-2008, 12:35 AM
  4. using swap to make assignment operator exception safe
    By George2 in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2008, 06:32 AM
  5. Replies: 6
    Last Post: 08-04-2003, 11:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21