@Amit
char foo=157; //10 01 11 01
unsigned char temp;
unsigned char base=1;
int i;
for(i=7; i>0; i=i-2)
{
temp=foo & (base<<i); // saving the 7th, 5th, 3rd, 1st bit
printf("%x\n", temp);
foo = (~(base<<i) & foo) | (((base<<(i-1)) & foo)<<1); // moving 6th,
4th, 2nd, 0th bit to adjacnt position
foo = (~(base<<(i-1)) & foo ) | (temp>>1); // setting the 6th, 4th, 2nd,
0th from temp
}
printf("%d\n", foo);hope it works :)
o/p
foo=110 // 01 10 11 10
Mohit
On Mon, Jun 21, 2010 at 5:37 PM, amit <[email protected]> wrote:
> Given a byte, write a code to swap every two bits. [Using bit
> operators] Eg: Input: 10 01 11 01 Output: 01 10 11 10
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<algogeeks%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.