Reverse bits

Reverse bits of an unsigned integer.

C++:
unsigned int reverseInt(unsigned int a)
{
   int size=sizeof(unsigned int)*8;
   for(int i=0;i<size/2;i++){
       if((a&(1<<i))^(a&(1<<(size1i))))
           a^=(1<<i)|(1<<(size1i));
   }
   return a;
}
Advertisements
This entry was posted in Bit operation. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s