Posted By

zava on 08/03/10


Tagged

textmate binary convert


Versions (?)

convert int to binary in c


 / Published in: C
 

  1. #define WIDTH 32
  2.  
  3. const char *int_to_binary ( int x )
  4. {
  5. static char b[WIDTH + 1] = {0};
  6. for (int z = WIDTH; z > 0; z--)
  7. {
  8. b[z - 1] = ((1 & x) == 1) ? '1' : '0';
  9. x >>= 1;
  10. }
  11. return b;
  12. }
  13.  
  14. int main()
  15. {
  16. unsigned int i = 0;
  17. i = ~i;
  18. printf("%s\n", int_to_binary(i));
  19. return 0;
  20. }

Report this snippet  

You need to login to post a comment.