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]);
}