Posted By

zava on 08/13/10


Tagged

textmate reverse bits


Versions (?)

reverse_bits.c


 / Published in: Other
 

  1. unsigned int reverse_bits(unsigned int value)
  2. {
  3. int size = sizeof(value) * 8;
  4. unsigned char flag = 0;
  5. unsigned int reversed = 0;
  6. while (size-- > 0)
  7. {
  8. flag = ((value & 1) == 1);
  9. value >>= 1;
  10. reversed |= flag;
  11. if (size > 0) reversed <<= 1;
  12. }
  13. return reversed;
  14. }
  15.  
  16. #define WIDTH 32
  17.  
  18. const char *int_to_binary ( int x )
  19. {
  20. static char b[WIDTH + 1] = {0};
  21. for (int z = WIDTH; z > 0; z--)
  22. {
  23. b[z - 1] = ((1 & x) == 1) ? '1' : '0';
  24. x >>= 1;
  25. }
  26. return b;
  27. }
  28.  
  29. int main()
  30. {
  31. printf("%un", reverse_bits(25));
  32. printf("%sn", int_to_binary(25));
  33. printf("%sn", int_to_binary(reverse_bits(25)));
  34. return 0;
  35. }

Report this snippet  

You need to login to post a comment.