**Reverse bits of an unsigned integer.**

C++:

1 unsigned int reverseInt(unsigned int a)

2 {

3 int size=sizeof(unsigned int)*8;

4 for(int i=0;i<size/2;i++){

5 if((a&(1<<i))^(a&(1<<(size–1–i))))

6 a^=(1<<i)|(1<<(size–1–i));

7 }

8 return a;

9 }

2 {

3 int size=sizeof(unsigned int)*8;

4 for(int i=0;i<size/2;i++){

5 if((a&(1<<i))^(a&(1<<(size–1–i))))

6 a^=(1<<i)|(1<<(size–1–i));

7 }

8 return a;

9 }

Advertisements