Why ^ will swap values?

This is a discussion on Why ^ will swap values? within the C Programming forums, part of the General Programming Boards category; >Why? Any side effects? I'll start with this hideous construct: a ^= b ^= a ^= b; Notice that a ...

  1. #16
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Why? Any side effects?
    I'll start with this hideous construct:

    a ^= b ^= a ^= b;

    Notice that a is changed more than once between sequence points, which makes the entire expression undefined. It's similar to a[x] = ++x;.

    But assuming that you declare it properly, like so:

    a ^= b;
    b ^= a;
    a ^= b;

    This is valid C code, but it doesn't work for general data types, or even all possible values of the data types it appears to work for. Conclusion? Avoid swap hacks like this because they are too dangerous for real programs.

    -Prelude
    My best code is written with the delete key.

  2. #17
    Registered User
    Join Date
    Feb 2002
    Posts
    51
    Hey all,

    Just wondering if it is also bad to use the following:

    a += b;
    a -= b = a - b;

    Our teacher asked us how to switch two variables without using any others, and said this answer was "simple minded" when I gave it and not the xor version.

    Is this really much worse (if any), besides the fact that it takes all of two to three more keystrokes.


    P.S. Please don't flame me too bad if this really is worse than the other. My ego can't take much more, lol.
    Last edited by drharv; 04-23-2002 at 01:58 AM.

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,769
    > Just wondering if it is also bad to use the following:
    Yes

    > Our teacher asked us how to switch two variables without using any others
    Wonder if you will learn anything useful, as opposed to a bunch of silly tricks

    Swapping variables without temporaries ceased to be of any use when we left assembler and started using C.

    Code:
    #include <stdio.h>
    
    void s1 ( int a, int b ) {
        a ^= b;
        b ^= a;
        a ^= b;
        printf( "%d %d\n", a, b );
    }
    void s2 ( int a, int b ) {
        int temp = a;
        a = b;
        b = temp;
        printf( "%d %d\n", a, b );
    }
    
    int main ( ) {
        s1( 1, 2 );
        s2( 1, 2 );
        return 0;
    }
    Guess which one is more efficient when you compile this?
    gcc -S prog.c

    Guess which one is more efficient when you turn on the optimiser?
    gcc -S -O prog.c

    In both cases, the results are in prog.s

    User's of GUI front end compilers will have to RTM to find out.



    Guess which one is clearer in it's intent to the average reader?

    Guess which one will work with types other than int?


    > Is this really much worse (if any),
    You might be relying on unspecified overflow and underflow properties of integers

  4. #19
    Registered User
    Join Date
    Feb 2002
    Posts
    51
    I tried the commands you suggested and that is very interesting. A very good lesson that less code does not always equal better code. Thanks.

  5. #20
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Shiro
    If I am correct it only applies to integer values and not to floating point values.
    How come? All variables are represented as a serie of 1's and 0's. Shouldn't be any difference if 10101011 is an int or a float... I think...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #21
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,769
    > How come?
    Easy, just define two floats, and try and use the ^ operator

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

Similar Threads

  1. Replies: 12
    Last Post: 04-12-2009, 06:49 PM
  2. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-31-2009, 12:53 AM
  3. Need help with project
    By chrisa777 in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2006, 06:01 PM
  4. ascii values for keys
    By acid45 in forum C Programming
    Replies: 2
    Last Post: 05-12-2003, 08:13 AM
  5. Checking ascii values of char input
    By yank in forum C Programming
    Replies: 2
    Last Post: 04-29-2003, 08:49 AM

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