How good your knowledge of bitwise tricks? Exercise.

I searched for rotate left/right function in pure C and found this:

// https://blog.regehr.org/archives/1063
uint32_t rotl32 (uint32_t x, uint32_t n)
{
        assert (n<32);
        return (x<<n) | (x>>(-n&31));
}

Ouch! John Regehr provided only rotate left function without corresponding right one. But I can manage this! I can reimplement rotr() using rotl(). My function is:

uint32_t rotr32_my(uint32_t x, uint32_t n)
{
        return ??? rotl32(?, ?) ???;
};

In my case, the length of "return ??? rotl32(?, ?) ???;" is 23 characters. If you can reimplement rotate right using only rotate left, your solution shouldn't be any longer.

In the same way, ROR x86 assembly instruction can be implemented using ROL. How?

(the post first published at 20220914.)


List of my other blog posts.

Yes, I know about these lousy Disqus ads. Please use adblocker. I would consider to subscribe to 'pro' version of Disqus if the signal/noise ratio in comments would be good enough.