Re: [algogeeks] Reverse Bits

2011-08-05 Thread rajeev bharshetty
@mithun : Thanks

On Fri, Aug 5, 2011 at 6:37 PM, mithun bs  wrote:

> Hi Rajeev,
>
> I follow similar approach. The basic logic is swap bits of a pair, then
> swap nibbles(2 bits) and then swap (4bits), 8bits  and go on.
>
> So for ex. 0110 1101 1100 0101
> In first step, I swap bits of each pair. So this becomes,
>
> Input -  0110 1101 1100 0101
> output- 1001 1110 1100 1010
>
> In second step, I swap 2bits
>
> Input - 1001 1110 1100 1010
> output-0110 1011 0011 1010
>
> I swap 4 bits now
>
> Input - 0110 1011 0011 1010
> output-1011 0110 1010 0011
>
> Now I swap 8bits
> Input - 1011 0110 1010 0011
> output-1010 0011 1011 0110
>
> So, now we have the bits in reverse order.
>
> First step I do this way
> x = ((x | 0x) >> 2) | (x<<2) | 0x;
> Next step similarly
> x = ((x | 0x) >> 4) | (x<<4) | 0x;
>
> This is the logic.
> Your code does the reverse way.
>
> Regards,
> Mithun
>
> On Fri, Aug 5, 2011 at 6:04 PM, rShetty  wrote:
>
>> This is the code to reverse the bits in an unsigned integer .
>> Could anyone please explain the logic of this approach ? Thank You !!
>>
>> #define reverse(x) \
>> (x=x>>16|(0x&x)<<16, \
>> x=(0xff00ff00&x)>>8|(0x00ff00ff&x)<<8, \
>> x=(0xf0f0f0f0&x)>>4|(0x0f0f0f0f&x)<<4, \
>> x=(0x&x)>>2|(0x&x)<<2, \
>> x=(0x&x)>>1|(0x&x)<<1)
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to algogeeks@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.
>>
>>
>
>
> --
> Mithun.B.S
> M:9916775380
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@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.
>



-- 
Regards
Rajeev N B 

"*Winners Don't do Different things , they do things Differently"*

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@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.



Re: [algogeeks] Reverse Bits

2011-08-05 Thread mithun bs
Hi Rajeev,

I follow similar approach. The basic logic is swap bits of a pair, then swap
nibbles(2 bits) and then swap (4bits), 8bits  and go on.

So for ex. 0110 1101 1100 0101
In first step, I swap bits of each pair. So this becomes,

Input -  0110 1101 1100 0101
output- 1001 1110 1100 1010

In second step, I swap 2bits

Input - 1001 1110 1100 1010
output-0110 1011 0011 1010

I swap 4 bits now

Input - 0110 1011 0011 1010
output-1011 0110 1010 0011

Now I swap 8bits
Input - 1011 0110 1010 0011
output-1010 0011 1011 0110

So, now we have the bits in reverse order.

First step I do this way
x = ((x | 0x) >> 2) | (x<<2) | 0x;
Next step similarly
x = ((x | 0x) >> 4) | (x<<4) | 0x;

This is the logic.
Your code does the reverse way.

Regards,
Mithun

On Fri, Aug 5, 2011 at 6:04 PM, rShetty  wrote:

> This is the code to reverse the bits in an unsigned integer .
> Could anyone please explain the logic of this approach ? Thank You !!
>
> #define reverse(x) \
> (x=x>>16|(0x&x)<<16, \
> x=(0xff00ff00&x)>>8|(0x00ff00ff&x)<<8, \
> x=(0xf0f0f0f0&x)>>4|(0x0f0f0f0f&x)<<4, \
> x=(0x&x)>>2|(0x&x)<<2, \
> x=(0x&x)>>1|(0x&x)<<1)
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@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.
>
>


-- 
Mithun.B.S
M:9916775380

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@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.



Re: [algogeeks] Reverse Bits

2011-08-05 Thread Nitin Nizhawan
x = x>>16 | (0x&x)<<16
this line exchanges ls 16bits with ms 16bits, i.e. 1 pair of 16bit

this logic of exchanging bits is the used for 2 pairs of 8bits each, then
for 4 pairs of 4bit, then for 8 pairs of 2 bit and finally 16 pairs of 1bit.


On Fri, Aug 5, 2011 at 6:04 PM, rShetty  wrote:

> This is the code to reverse the bits in an unsigned integer .
> Could anyone please explain the logic of this approach ? Thank You !!
>
> #define reverse(x) \
> (x=x>>16|(0x&x)<<16, \
> x=(0xff00ff00&x)>>8|(0x00ff00ff&x)<<8, \
> x=(0xf0f0f0f0&x)>>4|(0x0f0f0f0f&x)<<4, \
> x=(0x&x)>>2|(0x&x)<<2, \
> x=(0x&x)>>1|(0x&x)<<1)
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@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.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@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.