Thread: swap integer problem

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    5

    swap integer problem

    today i practised selection sorting algorithm with following

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define LEN 10
    
    void swap(int *a,int *b)
    {
    
            /*
            int temp = *a;
            *a = *b;
            *b = temp;
    
            */
            *a = *a ^ *b;
            *b = *a ^ *b;
            *a = *a ^ *b;
    }
    
    
    
    
    void selection_sort(int *arr, int len)
    {
            int i = 0;
           for(;i<len-1;i++)
            {
                    int j=i+1,min=i;
                    for(;j<len;j++)
                    {
                            if(arr[j] < arr[min])
                            {
                                    min = j;
                            }
                    }
                    swap(&arr[min],&arr[i]);
            }
    }
    
    int main()
    {
            int *a = malloc(sizeof(int) * LEN);
            int i = 0;
    
            for(;i<LEN;i++)
            {
                    a[i] = rand()%300;
                    printf("%d\t",a[i]);
            }
            printf("\n");
    
            selection_sort(a,LEN);
    
            for(i=0;i<LEN;i++)
            {
                    printf("%d\t",a[i]);
            }
            printf("\n");
            free(a);
    }

    if i use code block
    int temp = *a;
    *a = *b;
    *b = temp;

    csxia-laptop:/data/learning/C/DataStructure # ./selection_insert
    283 286 177 115 293 235 286 192 249 121
    115 121 177 192 235 249 283 286 286 293


    it works fine , but if i use the latter code block
    *a = *a ^ *b;
    *b = *a ^ *b;
    *a = *a ^ *b;
    then a and b's value will get zero if the address of a and b is the same

    csxia-laptop:/data/learning/C/DataStructure # ./selection_insert
    283 286 177 115 293 235 286 192 249 121
    115 121 0 192 235 249 283 0 286 293


    i have no idea why, anyone who can help me???

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by csxia
    it works fine , but if i use the latter code block
    *a = *a ^ *b;
    *b = *a ^ *b;
    *a = *a ^ *b;
    then a and b's value will get zero if the address of a and b is the same

    i have no idea why, anyone who can help me???
    Think about what happens when you write:
    Code:
    int x = 2;
    x = x ^ x;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    26
    Quote Originally Posted by csxia View Post
    but if i use the latter code block
    *a = *a ^ *b;
    *b = *a ^ *b;
    *a = *a ^ *b;
    then a and b's value will get zero if the address of a and b is the same
    i have no idea why, anyone who can help me???
    It's because XOR of equal numbers is 0.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    OMG this is a bug

    laserlight, thanks for your quick reply

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    100
    It is not a bug, that is how XOR works. The problem is in using an annoying XOR swap when it is a bad idea. Stick with your first swap code. Sure, it has a temp, so? It is clear, and straightforward.

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. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  3. Problem with cin. Please help me.
    By Antigloss in forum C++ Programming
    Replies: 17
    Last Post: 06-06-2005, 09:50 AM
  4. A homework problem about C++ (Pointer)
    By joenching in forum C++ Programming
    Replies: 10
    Last Post: 03-14-2005, 04:28 PM
  5. problem with random integer
    By techno logic in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2002, 02:20 PM

Tags for this Thread