Ive been trying to get the next one working in wich I have to switch bit 0--> 1, 1-->2, ... , 7-->0 and each bit has to hold it's own value when it get's switched.
Bit rotate?
[B]#include[/B] <stdio.h>
[B]#include[/B] <limits.h>
[B]int[/B] [B]main[/B]([B]void[/B])
{
[B]unsigned[/B] [B]char[/B] value = 0xAA;
[B]unsigned[/B] [B]char[/B] msb = 1 << (CHAR_BIT - 1);
/*
* First capture the MSB to rotate back into the LSB.
*/
[B]unsigned[/B] [B]char[/B] highbit = !!(value & msb); /* !! forces result to LSB */
/*
* Then shift all the bits of the value. The MSB "drops off the end".
*/
[B]unsigned[/B] [B]char[/B] result = value << 1;
/*
* "Rotate" in the former high bit.
*/
result |= highbit;
[B]printf[/B]("value = 0x%X, result = 0x%X\n", value, result);
/*
* Let's do that again!
*/
value = result; /* start again with the previous result */
highbit = !!(value & msb);
result = value << 1;
result |= highbit;
[B]printf[/B]("value = 0x%X, result = 0x%X\n", value, result);
[B]return[/B] 0;
}
/* my output
value = 0xAA, result = 0x55
value = 0x55, result = 0xAA
*/