How good your knowledge of bitwise tricks? Exercise. And answer.

I posted the exercise at 14-sep-2022, but no one solved it. Shame! Try to solve it now, then scroll down this page for answer.

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

(Scroll down.)

#include <limits.h>
#include <assert.h>
#include <stdio.h>
#include <stdint.h>

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

// https://stackoverflow.com/questions/776508/best-practices-for-circular-shift-rotate-operations-in-c
static inline uint32_t rotr32 (uint32_t n, unsigned int c)
{
        const unsigned int mask = (CHAR_BIT*sizeof(n) - 1);

        // assert ( (c<=mask) &&"rotate by type width or more");
        c &= mask;
        return (n>>c) | (n<<( (-c)&mask ));
}

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

void main()
{
        for (int i=1; i<=32; i++)
                for (int j=1; j<32; j++)
                {
                        uint32_t t1=rotr32(1<<i, j);
                        uint32_t t2=rotr32_my(1<<i, j);
                        assert (t1==t2);
                };
};

Not efficient. Not to be used in practice. But correct.

(the post first published at 20230331.)


List of my other blog posts.

Subscribe to my news feed

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.