thanks a lot for the wonderful explanation :-)

On Mon, Jun 27, 2011 at 7:17 PM, Dave <[email protected]> wrote:

> @Rajeev and Piyush: Numbering the bits from the right starting with 0
> as usual, you see that you need to move the even-numbered bits one bit
> to the left and the odd-numbered bits one bit to the right. You could
> do this one bit-pair at a time, but it would be more efficient if you
> could do all pairs simultaneously. So how can you pick out all of the
> even-numbered bits at the same time? By using the logical and of the
> data with a mask that has 0s in the odd bit positions and 1s in the
> even bit positions. I.e., anding x with 01010101 in binary, which is
> 0x55 in hexadecimal. Once you have isolated the even numbered bits,
> you shift them left one place. The result is (x & 0x55) << 1.
> Similarly, you want to isolate the odd-numbered bits and shift them
> right one place. You could do that by anding with the mask 10101010
> (binary) = 0xAA, and then shifting right: (x & 0xAA) >> 1. This
> introduces another constant, which might not be as efficient as
> reusing the previous constant 0x55, which can be done by shifting
> right first and then anding: (x >> 1) & 0x55. Now that we have the two
> sets of bits in the correct positions, we simply need to combine them,
> and the logical or operator does that just fine. The overall result is
> (x & 0x55) << 1) | ((x >> 1) & 0x55).
>
> Dave
>
> On Jun 27, 7:53 am, piyush kapoor <[email protected]> wrote:
> > Yep,I also want to know the same..
> >
> > On Mon, Jun 27, 2011 at 6:23 PM, rajeev bharshetty <[email protected]
> >wrote:
> >
> >
> >
> >
> >
> > > @ Dave How to think about the answer to the above question . I mean How
> do
> > > I tackle such problems ?
> >
> > > On Mon, Jun 27, 2011 at 6:17 PM, Dave <[email protected]> wrote:
> >
> > >> y = ((x & 0x55) << 1) | ((x >> 1) & 0x55).
> >
> > >> Note, 0x55 = 01010101 in binary.
> >
> > >> Dave
> >
> > >> On Jun 27, 7:18 am, rShetty <[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].
> > >> 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.
> >
> > --
> > *Regards,*
> > *Piyush Kapoor,*
> > *CSE-IT-BHU*- Hide quoted text -
> >
> > - Show quoted text -
>
> --
> 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.
>
>


-- 
*Regards,*
*Piyush Kapoor,*
*CSE-IT-BHU*

-- 
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.

Reply via email to