is tha good method?

This is a discussion on is tha good method? within the C Programming forums, part of the General Programming Boards category; i write the program: Code: #include "stdio.h" int main() { int a,b,c; a=1; b=2; c=0; c=a; a=b; a=c; /*........ and ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    4

    Question is tha good method?

    i write the program:
    Code:
    #include "stdio.h"
    
    int main()
    {
       int a,b,c;
       a=1;
       b=2;
       c=0;
    
       c=a;
       a=b;
       a=c;
    /*........ and other output command*/
     retrun 0;
    }
    but i found the Others write as:

    Code:
    #include "stdio.h"
    int main()
    {
      int a,b;
      a=1;
      b=2;
      a=a^b;
      b=a^b;
      a=a^b;
    /*...... and other output command*/
    return 0;
    }
    the second method is Good?

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    what exactly are you trying to do?
    Woop?

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    4
    swap two int variables.
    Last edited by MartinLiao; 07-23-2004 at 05:59 AM.

  4. #4
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    if you are trying to swap two variables, then no, xor'ing them is not a good method, b/c it works only for unsigned values.

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    ya your method is the one your going to want to use
    Woop?

  6. #6
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by MartinLiao
    i write the program:
    Code:
       c=a;
       a=b;
       a=c;
    Code:
      a=a^b;
      b=a^b;
      a=a^b;
    the second method is Good?
    The first method is simple and portable. I recommend it.
    The second tries to be smart, but is broken (actually, not portable with signed).
    Last edited by Emmanuel Delaha; 07-23-2004 at 09:45 AM. Reason: typos
    Emmanuel Delahaye

    "C is a sharp tool"

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    But the first method in this case has an error, it should be
    Code:
    c = a;
    a = b;
    b = c;
    (so basically a typo in the third line).
    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

  8. #8
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >but i found the Others write as
    Not if they're writing serious code. This nasty trick is annoyingly common and error prone. The biggest problem is that most people who use it are unaware of when it can be used and when it can't. Be suspicious of any code that swaps two values like that.

  9. #9
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    If you are swaping ints (signed or unsigned) this functions works, and doesn't use an temporary variable (thus faster):

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

    Proof:

    a = -5
    b = 2

    a = -5 + 2 = -3
    b = -3 - 2 = -5
    a = -3 - (-5) = 2

    a = 2
    b = -5

    Should also work handle overflow. You can produce the proof by useing a lower overflow level like -10 to 10:

    a = 7
    b = 6

    a = 7 + 6 = -8 (remember wrap around when above 10 or below - 10 in this proof...)
    b = -8 - 6 = 7
    a = -8 - 7 = 6

    a = 6
    b = 7

    Theis method does of course not work correctly when swapping floats due to the way a float variable works.

  10. #10
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Ninebit, your method could fail if the addition or subtraction goes out of the range of an int. For example, if:
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main(void) {
            int a = INT_MAX;
            int b = 20;
    
            a = a + b;
            b = a - b;
            a = a - b;
            printf("a = %d, b = %d.\n", a, b);
            return 0;
    }
    My result:
    a = 20, b = 2147483647.
    It works in pure math, but fails with computer representation of numbers.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,447
    > and doesn't use an temporary variable (thus faster):
    I very much doubt it.
    Every time I've tried these "cute" methods, all they do is confuse the compiler optimiser (as well as the average reader).

    Also, signed integer overflow / underflow produces undefined behaviour.

    Stick to using a temporary - it always works, it works with any type and the compiler optimiser will see what you are doing. With any luck, it will rearrange the following code to effectively eliminate the swap from ever happening.

    Here's just one way in which clever tricks outwit the programmer
    Code:
    #include <stdio.h>
    
    void swapper ( int *a, int *b ) {
        *a ^= *b;
        *b ^= *a;
        *a ^= *b;
    }
    
    int main ( ) {
        int a = 10, b = 20;
        printf( "%d %d\n", a, b );
        swapper( &a, &b );
        printf( "%d %d\n", a, b );
        swapper( &a, &a );  /* with itself */
        printf( "%d %d\n", a, b );
        return 0;
    }
    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.

  12. #12
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    Oh, and by the way, xor swap does work with signed values... as opposed to what some other people here says... bit-fields does not care about what number it represent... just run this smal program testing it yourself...

    Code:
    #include "stdio.h"
    
    int main(int argc, char* argv[])
    {
    	signed int a = -10; // signed just for the sake of it
    	signed int b = 5;
    
    	printf("a = %d\nb = %d\n", a, b);
    	printf("swapping\n");
    
      	a ^= b;
    	b ^= a;
    	a ^= b;
    
    	printf("a = %d\nb = %d\n", a, b);
    
    	return 0;
    }

  13. #13
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    Nope it does work both computational and mathematically

    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main(void) {
            int a = INT_MAX;
            int b = 20;
    
            printf("a = %d, b = %d.\n", a, b); // <-- added this line
            a = a + b;
            b = a - b;
            a = a - b;
            printf("a = %d, b = %d.\n", a, b);
    		while(1);
            return 0;
    }
    result:
    a = 2147483647, b = 20.
    a = 20, b = 2147483647.

  14. #14
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    You forgot that INT_MAX is half of 4294967295...

  15. #15
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    And Salem... it is faster...

    by using same way of thinking as you:

    pure + - ops does not need to be optimized by compiler its already as fast as it can be.
    the compiler can and will rearrange the code so that the swap did never happen if the code alows it (this is of cause compiler dependent exactly as much as any other temp variable rearrange is...)
    Temp variable swap takes more time because of the temp variable creation, not the algorithm...

    But i agree that xor should be avoided...
    Last edited by ninebit; 07-25-2004 at 12:10 PM.

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

Similar Threads

  1. stuck on display method
    By shintaro in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2009, 04:17 PM
  2. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-07-2008, 11:30 PM
  3. C# method
    By siten0308 in forum C# Programming
    Replies: 6
    Last Post: 07-15-2008, 07:01 AM
  4. Overriding a method in C
    By DavidDobson in forum C Programming
    Replies: 1
    Last Post: 07-05-2008, 07:51 AM
  5. Need a good method
    By Asagohan in forum Tech Board
    Replies: 11
    Last Post: 04-08-2005, 04:57 PM

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