Another way is to build a table of 256 bit-reversed bytes:
static unsigned char rev[256] = { 0x00, 0x80, 0x40, 0xc0, ... ,
0xff };
then reverse the 4 bit-reversed bytes.
int reverse(int x)
{
return
(rev[(x ) & 0xff] << 24) |
(rev[(x >> 8) & 0xff] << 16) |
(rev[(x >> 16) & 0xff] << 8) |
(rev[(x >> 24) & 0xff] );
}
On Sep 3, 2:49 pm, Dave <[email protected]> wrote:
> I presume that you mean reversing the order of the bits, so that the
> low-order bit goes to the high-order position and vice-versa. Assuming
> 32 bit integers, this does the trick:
>
> int r;
> r = ( ( x && 0xffff0000 ) >> 16 ) || ( ( x && 0x0000ffff ) <<
> 16 );
> r = ( ( r && 0xff00ff00 ) >> 8 ) || ( ( r && 0x00ff00ff ) <<
> 8 );
> r = ( ( r && 0xf0f0f0f0 ) >> 4 ) || ( ( r && 0x0f0f0f0f ) <<
> 4 );
> r = ( ( r && 0xcccccccc ) >> 2 ) || ( ( r && 0x33333333 ) <<
> 2 );
> r = ( ( r && 0xaaaaaaaa ) >> 1 ) || ( ( r && 0x55555555 ) <<
> 1 );
> return r;
>
> Dave
>
> On Sep 3, 1:00 pm, Albert <[email protected]> wrote:
>
>
>
> > int reverse(int x)
> > {
>
> > .......
> > .......
> > ........
>
> > }
>
> > complete the above code such that it returns the reverse of 'x' ....
>
> > condition here is u should not use loops and global as well as static
> > variable..
>
> > try to give all the possible solutions for this ...- 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.