Q:

How to make specific bit manipulation?

I'm doing practice of bit manipulation in arduino with a 74HC595 shift register.
I would like to create an algorithm that allows the binary digit to perform this way:
1 0 0 0 0 0 0 1
0 1 0 0 0 0 1 0
0 0 1 0 0 1 0 0
.
.
.
1 0 0 0 0 0 0 1
In this type of function the decimal values are: (129,66,36,24,24,36,66,129) and so on in a loop.
How can I perform this type of shifting? I don't have any fluency thinking this type of operation, I have only performed a circular shift with "an algorithm" like:
//my circular shift

myByte = myByte*128 + myByte/2

But I don't know how to perform the output that I showed.
How can I do this? Thanks

A:

For example you can use the following approach
#include <iostream>
#include <iomanip>
#include <limits>

int main() 
{
    unsigned char b = 0b10000001;
    int width = std::numeric_limits<unsigned char>::digits / 2;

    for ( int i = 0; i < width; i++ )
    {
            std::cout << std::hex << static_cast<int>( b ) << " - "
                      << std::dec << static_cast<int>( b ) << '\n';
            b = ( b & ( 0b1111 << width ) ) >> 1 | ( b & 0b1111 ) << 1;
    }

    for ( int i = 0; i < width; i++ )
    {
            std::cout << std::hex << static_cast<int>( b ) << " - "
                      << std::dec << static_cast<int>( b ) << '\n';
            b = ( b & ( 0b1111 << width ) ) << 1 | ( b & 0b1111 ) >> 1;
    }

    return 0;
}

The program output is
81 - 129
42 - 66
24 - 36
18 - 24
18 - 24
24 - 36
42 - 66
81 - 129

