Let me explain, supposing that you haven't really tried to understand the code. The first logical product picks out bits 1, 3, 5, and 7 and shifts them 1 position to the right. The second logical product picks out bits 0, 2, 4, and 6 and shifts them 1 position to the left. Then just or the two sets of bits together.
Dave On Jun 21, 4:05 pm, Amit Jaspal <iit2007...@iiita.ac.in> wrote: > @ Dave would u plz bother to discuss how do u arrive at this formula? > > > > On Mon, Jun 21, 2010 at 10:11 PM, Dave <dave_and_da...@juno.com> wrote: > > Not hard at all: > > > y = ((x & 0xAA) >> 1) | ((x & 0x55) << 1) > > > Dave > > > On Jun 21, 7:07 am, amit <amitjaspal...@gmail.com> 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 algoge...@googlegroups.com. > > To unsubscribe from this group, send email to > > algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@googlegroups.com> > > . > > For more options, visit this group at > >http://groups.google.com/group/algogeeks?hl=en.- 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 algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.