Skip to content

Instantly share code, notes, and snippets.

@rupalbarman
Last active April 9, 2021 17:19
Show Gist options
  • Save rupalbarman/3c46a6197c59287b52f0c715d2b61d9f to your computer and use it in GitHub Desktop.
Save rupalbarman/3c46a6197c59287b52f0c715d2b61d9f to your computer and use it in GitHub Desktop.
Rotate an Array using XOR operations
/* The code allows rotation of an 1D array (right and left)
using XOR operator
ie. Shifts elements of an Array to left or right directions,
wraps the elements circularly.
*/
#include <stdio.h>
int arr[]={1,2,3,4,5,6,7,8,9,10};
void rotateLeft()
{
int i;
for(i=0;i<10-1;i++)
arr[i]=arr[i]^arr[i+1]^(arr[i+1]=arr[i]);
}
void rotateRight()
{
int i;
for(i=10-1;i>0;i--)
arr[i]=arr[i]^arr[i-1]^(arr[i-1]=arr[i]);
}
void show()
{
int i;
printf("\n");
for(i=0;i<10;i++)
printf("%d\t",arr[i]);
}
int main()
{
show();
rotateLeft();
show();
rotateRight();
show();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment