swap of variables without tmp

This is a discussion on swap of variables without tmp within the C Programming forums, part of the General Programming Boards category; Is it possible? Some months ago, a programmer told me it is, but he didn't tell me how. I'm still ...

  1. #1
    ZaC
    ZaC is offline
    Lost in the C ZaC's Avatar
    Join Date
    Jun 2008
    Location
    Italy
    Posts
    47

    swap of variables without tmp

    Is it possible?
    Some months ago, a programmer told me it is, but he didn't tell me how. I'm still thinking about it.
    Thanks in advance
    Sorry for my bad English
    and also for my bad programming style...

    ZaC'ZaCoder (?!)

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Try searching the forum or internet for it. You know, with key words that are similar to your title...


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ZaC View Post
    Is it possible?
    Yes. You need a phenomenal sense of balance tho*, so we try to encourage people to just go pedestrian and use the "tmp".

    *because, logically, the variables must pass each other in mid air
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Apprentice
    Join Date
    Oct 2006
    Location
    LA
    Posts
    53
    Here's one way to swap 2 integer variables a and b.

    Code:
    a= a+b
    b= a-b
    a= a-b

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    undisclosed
    Posts
    5
    Problem w/ the above code is that it might cause overflows.

    Code:
    a = a^b;
    b = a^b;
    a = a^b;

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,551
    > Problem w/ the above code is that it might cause overflows.
    Problem with the above code, it's only guaranteed on unsigned ints.
    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.

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    Now that you know, treat it like a piece of trivia.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or they could have just searched, learned ALL of this, because it's been posted a hundred times here, and saved each one of us the trouble of typing it all yet again!


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,551
    Give it time, we haven't seen the super-compressed (and even more broken) 1-liner XOR swap
    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.

  10. #10
    ZaC
    ZaC is offline
    Lost in the C ZaC's Avatar
    Join Date
    Jun 2008
    Location
    Italy
    Posts
    47
    Quote Originally Posted by quzah View Post
    Try searching the forum or internet for it. You know, with key words that are similar to your title...


    Quzah.
    You are right, it was very late and sleep made me to write without searching (I use it, don't you worry)

    Quote Originally Posted by quzah View Post
    Or they could have just searched, learned ALL of this, because it's been posted a hundred times here, and saved each one of us the trouble of typing it all yet again!


    Quzah.
    what a big trouble... if you are busy just don't care

    Quote Originally Posted by Salem View Post
    Give it time, we haven't seen the super-compressed (and even more broken) 1-liner XOR swap
    it's what I'm looking for, I already know the lazyme method, but becouse of my poor english and of my tiredness I forgot to write you about it.
    So is not good the 1-linear xor swap?
    Sorry for my bad English
    and also for my bad programming style...

    ZaC'ZaCoder (?!)

  11. #11
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    I never knew poor English makes people forget things...

    Because of my poor German I forgot to feed my dog.

  12. #12
    ZaC
    ZaC is offline
    Lost in the C ZaC's Avatar
    Join Date
    Jun 2008
    Location
    Italy
    Posts
    47
    Code:
    if ((enlgish == poor) && tired) printf("%s",few);
    else printf("%s",more);
    Sorry for my bad English
    and also for my bad programming style...

    ZaC'ZaCoder (?!)

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ZaC View Post
    So is not good the 1-linear xor swap?
    You are only allowed to change a variable once in a sequence point [1], and the one-liner xor swap does modify the same variable more than once (because of the way that it swaps x with y, it changes x twice). This is not forbidded to write in the compiler, it is what is called "undefined behaviour" - the compiler may treat do "what it feels like" in this sequence of operations - it may decide to break it up in ways you wouldn't expect. Undefined behaviour means "anything can happen": nuclear missiles launch, incorrect result, or even correct result. It's a bit like driving across a junction when the light is red in your direction. If you are lucky, nothing bad happpens. If you have a bit of luck, you have a small collision, and if you are really unlucky, there is a BIG lorry crossing in the other direction, and your car is crushed.

    [1] Sequence point is normally a statement in C, but there are some exceptions. Basically, a sequence point is the point at which the preceding code is completed, so if you say:
    Code:
    a = b + c + d;
    the semicolon indicates the sequence point. If you were to look at the place where a is stored somewhere WITHIN the code generated for that line, it may contain the old value before that line, some partial value, or the whole sum of b + c + d, depending on where you look at it [and it may contain some spurious intermediate results at some point too, such as c + d or b + c, or even b + d, depending on how the compiler composes the code].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Jun 2009
    Location
    undisclosed
    Posts
    5
    Quote Originally Posted by Salem View Post
    Give it time, we haven't seen the super-compressed (and even more broken) 1-liner XOR swap
    Code:
    b = (a ^= (b ^= a)) ^ b;  
    a ^= b ^= a ^= b; // O_O
    *read matsp post above for problems...

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    2,037
    You can do xor swap just fine in two lines:
    Code:
    b ^= a ^= b;
    a ^= b;
    And then of course there is this:
    Code:
    b ^= a ^= b, a ^= b;
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 11:02 AM
  2. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 03:46 PM
  3. Swap two variables with out third variable
    By nkanthikiran in forum C Programming
    Replies: 3
    Last Post: 01-30-2005, 12:33 PM
  4. hwnd and variables in them
    By underthesun in forum Windows Programming
    Replies: 6
    Last Post: 01-16-2005, 05:39 PM
  5. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 01:39 PM

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