Thread: Algorithm to rotate an array does not work, whats wrong?

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

    Algorithm to rotate an array does not work, whats wrong?

    I'm trying to get a grasp of what this algorithm does such that it rotates an array's elements. For example an array with elements
    Code:
    {1,2,3,4,5,6,7,8,9,10}
    is rotated to output
    Code:
    {4,3,2,1,10,9,8,7,6,5}
    In particular there is a portion of the array that is XOR'd but done so in a way that is confusing to me. I'll post the code here, could someone please explain what the program is performing when XORing the elements in the array? Thanks!

    Code:
    #include <stdio.h>
    void reverse_array(int[], int);
    void rotate_array(int[], int, int);
    int main(void) {
        int arr[] = {1,2,3,4,5,6,7,8,9,10};
        rotate_array(arr, 10, 2);//reverse array, not sure what parameters to pass here...
        return 0;
    }
    
    void rotate_array(int arr[], int n, int k) {
        reverse_array(arr,k);
        reverse_array(&arr[k],n-k);
        reverse_array(arr,n);
    }
    
    void reverse_array(int arr[], int n) {
        int j = n-1;
        for(int i = 0;i < j; i++, j--) {
    
            arr[i]^=arr[j]^=arr[i]^=arr[j];//what is happening here??
        
        }
        
        for(int k = 0; k < 10; k++)
           printf("value = %d\n", arr[k]);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > arr[i]^=arr[j]^=arr[i]^=arr[j];
    Question 3.3b

    It's someone's stupid trick to swap two variables without using a temporary variable.

    Except in this case, it screws up badly if you happen to try to swap a variable with itself.

    Just do
    int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
    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.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    The code works for me. If it doesn't work for you, it could only be because the xor trick has been written in a way that is UB. It should be split into three lines:
    Code:
       a ^= b;
       b ^= a;
       a ^= b;
    Actually, there's a tad more UB since you print beyond the bounds of the array in the second call to reverse_array (when the input array is actually only 8 ints long). The print code should be moved to main.

    The purpose of the xor trick is to avoid a temporary variable, which doesn't save you much. Modern compilers should recognize idiomatic swap code and optimize it anyway. And, as Salem said, it doesn't work if the two variables refer to the same object. So a normal swap should be at least as efficient and more generally useful.

    The trick leverages the fact that xoring by the same value twice yields the original value, e.g., 17 ^ 5 ^ 5 == 17. It just does it between two variables. It's also important that it's commutative. Unpacking the combined assignment operators and rearranging slightly makes it clearer:
    Code:
       a = a ^ b;
       b = a ^ b; // is b = a ^ b ^ b using original values
       a = a ^ b; // is a = a ^ b ^ a using original values

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Whats wrong with my sorting algorithm?
    By bungkai in forum C Programming
    Replies: 7
    Last Post: 01-20-2012, 04:35 PM
  2. What's wrong with STL rotate algorithm?
    By Mathsniper in forum C++ Programming
    Replies: 3
    Last Post: 12-09-2006, 08:54 AM
  3. Whats wrong with my array?
    By ammochck21 in forum C++ Programming
    Replies: 8
    Last Post: 12-05-2006, 01:11 PM
  4. Whats wrong with my array?
    By Munkey01 in forum C++ Programming
    Replies: 23
    Last Post: 03-26-2003, 06:07 AM
  5. Replies: 3
    Last Post: 01-19-2003, 04:08 PM

Tags for this Thread