fork(3) download
  1. #include <stdio.h>
  2.  
  3. void printBinaryValue2(unsigned int num)
  4. {
  5. char result[sizeof(num)*8];
  6. int count = 0;
  7. while(num)
  8. {
  9. result[count++] = ((num&1 == 1) ? '1' : '0');
  10. num>>=1;
  11. }
  12. if(count)
  13. {
  14. count--;
  15. while(count>=0)
  16. {
  17. putchar(result[count--]);
  18. }
  19. }
  20. else
  21. {
  22. putchar('0');
  23. }
  24. }
  25.  
  26. void printBinaryValue(unsigned int num)
  27. {
  28. if(!num) return;
  29.  
  30. printBinaryValue(num>>1);
  31. putchar(((num&1 == 1) ? '1' : '0'));
  32. }
  33.  
  34.  
  35. int main(void) {
  36. // your code goes here
  37. printBinaryValue(-9);
  38. putchar('\n');
  39. printBinaryValue(0);
  40. putchar('\n');
  41. printBinaryValue(9);
  42. putchar('\n');
  43. printBinaryValue2(-9);
  44. putchar('\n');
  45. printBinaryValue2(0);
  46. putchar('\n');
  47. printBinaryValue2(9);
  48. putchar('\n');
  49. printBinaryValue2(1);
  50. return 0;
  51. }
  52.  
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
11111111111111111111111111110111

1001
11111111111111111111111111110111
0
1001
1