I explain my problem.
For example considering a 8 bit integer, to obtain symmetric number around "middle bits", bits 0 and 7 are swapped, bits 1 and 6 are swapped, bit 2 and 5 are swapped and bits 3 and 4 are swapped. (ex. 10100110 => 01100101)
The sample principal applies whatever the integer length.
Following this answer(https://stackoverflow.com/a/982139/7462275), I wrote this code that gives the expected result.
int bit_lg=8; // 8 bits (from 0 to 7)
unsigned int my_int=0b10100110, my_res=my_int;
unsigned short xbit;
for (int i=0; i<bit_lg/2;i++) {
xbit = ((my_res >> i) ^ (my_res >> (bit_lg-1-i))) & 0b1;
my_res = my_res ^ ((xbit << i) | (xbit << (bit_lg-1-i)));
}
printf("%B : %.8B\n",my_int,my_res);
I wonder if it possible to avoid the for
loop. And obtained the result in one step ?
Perhaps does it exist some __builtin_
(gcc is used) ?