-
-
Save ronerlih/a254aa5a3601545519b1ae76c67ed695 to your computer and use it in GitHub Desktop.
Rotate an Array using XOR operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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