#include
#include
/* Write a function returning the maximum of all values obtained by repeated
application of swaprot() to a 32-bit int.
swaprot computes a value as follows: swap the two 16-bit halves, then
rotate right by 4 bits (rightmost bits are inserted leftmost). */
int32_t swaprot(uint32_t n) // work with uint32_t for >>
{
printf("%x\n", n); // just to visualize
n = n << 16 | n >> 16;
return n << 28 | n >> 4; // combined: rotl 12: n << 12 | n >> 20
}
unsigned maxrot(int32_t n) // "32-bit int" means signed
{
int32_t crt = n, max = n;
while ((crt = swaprot(crt)) != n)
if (crt > max) max = crt;
return max;
}
int main(void)
{
printf("\n%x\n", maxrot(0x12345678)); // 0x7... since 0x8... is negative
}