Create a function to get the value of the integer x rotated to the right by n positions. - C Function

C examples for Function:Utility Function

Description

Create a function to get the value of the integer x rotated to the right by n positions.

Demo Code

#include <stdio.h>

int rightroc(int x, int n);

int main(void){
    printf("%u\n", rightroc(5432, 3));
    return 0;/*from   w  w w  . ja  va2 s  . c o m*/
}

int rightroc(int x, int n){   
    while (n-- > 0){
        if(x & 1)
            x = (x >> 1) | ~(~0U >> 1);
        else
            x = x >> 1;
    }
    return x;
}

Related Tutorials