Bit rotations exercise I'm studying C on K&amp;R and I solved exercise 2.08:


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


I've tested my code with some bit patterns and it seems to work, but I'm not sure that this solution covers all possible cases.

What do you think about this code?

unsigned rightrot(unsigned x, int n)
{   
    int size;
    unsigned y;

    size = 0;
    y = x;

    while (y != 0) {
    y = y &lt;&lt; BYTESIZE;
    size++;
    }
    size = size * BYTESIZE;

    return (x &lt;&lt; (size-n)) | (x &gt;&gt; n);
}


Here is the main

#include &lt;stdio.h&gt;

#define BYTESIZE 8

unsigned rightrot(unsigned x, int n);

int main(void)
{
     unsigned x;
     int n;

     x = 0x23acb;
     n = 2;

     printf("%x\n", rightrot(x, n));

     return 0;
}
Isn't it something like this?

#include &lt;limits.h&gt; /* for CHAR_BIT */

unsigned
rotate_right(unsigned x, int n)
{
    int left_shift = ((sizeof x) * CHAR_BIT) - n;
    return x&lt;&lt;left_shift | x&gt;&gt;n;
}