I thought that an operator performs a permanent change in a local variable. For example, if x is 00000000 00000000 00000000 00000011 (a 32 bit unsigned integer that resolves to value of 3) and p is 2, in the expression "return (x >> p) | (x << (s - p))", the right shift would permanently change x to 0 and the time we evaluate x again in "(x << (s - p))", x will already be 0. If that's the case, then this function doesn't make sense. This function makes it seem like that the >> and << operators do not change the value of x. It makes it seem like first we right shift x to 0 and then left shift by 20 bits to make x 11000000 00000000 00000000 00000000. If that's the case, then the function does exactly what it is supposed to do (rotate the bits). So which is it?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
unsignedint rightrot(unsignedint x, unsignedint n)
{
size_t s = sizeof(x) * CHAR_BIT;
size_t p;
if(n < s)
p = n;
else
p = n % s;
if((0 == x) || (0 == p))
return x;
return (x >> p) | (x << (s - p));
}
An operator doesn't "change" a variable/object/value. You could think of it "returning" the result of the operation. I can tell you what my house would look like if I painted it red, but talking about it doesn't actually paint my house red.