Mega Code Archive

 
Categories / C / Development
 

Reverse bit order in an integer

#include <stdio.h> static unsigned long reverse(unsigned long x) { unsigned long h = 0; int i = 0; for(h = i = 0; i < 32; i++) { h = (h << 1) + (x & 1); x >>= 1; } return h; }