Write a function righrot(x, n) that returns the value of the integer x rotated to the right by n bit positions.

/*rightrot: rotate x to the right by  n positions     */
unsigned rightrot(unsigned x, int n){
        int wordlength(void);
        int rbit;     /*rightmost bit */
        while(n-- > 0){
             rbit = (x & 1) << (wordlength() - 1);
             x = x >> 1;       /*shift x 1 position right    */
             x = x | rbit;      /* complete one rotation */
        }
        return x;
}

/* wordlength: computes word length of the machine */
int wordlength(void){
     int i;
     unsigned v = (unsigned) ~0;
     for (i =1; (v = v >> 1)  > 0; i++)
               ;
     return i;
}


//Alternate solution
/*rightrot: rotate x to the right by  n positions   */
unsigned rightrot(unsigned x, int n){
      int wordlength(void);
      unsigned rbits;

      if ((n =n % wordlength()) > 0){
            rbits = ~(~0 <<n) & x;  /*n rightmost bits of x   */
                                      /*n rightmost bits to left */
            rbits = rbits <<(wordlength() - n);
             x = x >> n;      /* x shifted n positions right */
             x = x | rbits;    /* rotation completed */
      }
       return x;
}
/* ~0 << n  => all ones are shifted n positions to the left leaving n in the rightmost positions.
~(~0 << n) all ones are in the n rightmost positions

Leave a comment