Last active
April 9, 2021 17:19
-
-
Save rupalbarman/3c46a6197c59287b52f0c715d2b61d9f 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