print-binary.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. This program prints a binary representation of a decimal number.
  3. To do this, it create an array of ints. So to store the binary number of 12: 1100,
  4. the array of ints looks like
  5. {0,0,0,....,1,1,0,0}
  6. It is probably using a fairly poor algorithm of converting decimals to ints, but oh well.
  7. It finds the highest power of 2 less than the number, stores that
  8. in the array of ints, at the end, then subtracts to find the next power of 2...
  9. The algorithm is not funtional at all. I
  10. 12 --> binary
  11. (8 --> binary) + ((12 - 8) --> binary)
  12. 2^3
  13. {0,0,0...1,0,0,0} + (4 --> binary) + ((4 - 4) --> binary)
  14. 2^2
  15. {0,0,0...0,1,0,0} + 0
  16. {0,0,0...1,0,0,0}
  17. + {0,0,0...0,1,0,0}
  18. -----------------
  19. {0,0,0...1,1,0,0}
  20. */
  21. #include <math.h>
  22. #include <assert.h>
  23. #include <stdio.h>
  24. #include <stdbool.h>
  25. #define MAX_NUMBER 128
  26. short binary_number [MAX_NUMBER];
  27. /* This function takes an int and prints its binary representation */
  28. static int return_highest_power_of_2 (int i)
  29. {
  30. /* find the highest power of 2 <= i */
  31. short int n = 0;
  32. for (; pow(2 , n) <= i; ++n)
  33. {
  34. }
  35. return --n;
  36. }
  37. /* This function adds a power of 2 (n), to the binary number array.
  38. * ie:
  39. binary_number = {0,0,0...1,0,1,0}
  40. n = 2
  41. {0,0,0...1,0,1,0}
  42. + 1 0 0
  43. ----------------
  44. return -> {0,0,0...1,1,1,0}
  45. */
  46. static void special_plus (int n)
  47. {
  48. extern short binary_number [];
  49. for (int i = MAX_NUMBER - n - 1; i >= 0; i--) //this changes the value of N. That's a problem! FIXME
  50. {
  51. binary_number[i]++;
  52. if (binary_number[i] == 1)
  53. {
  54. break;
  55. }
  56. else if (binary_number[i] > 1)
  57. {
  58. binary_number[i] = 1;
  59. }
  60. else
  61. {
  62. //binary_number[i] should be greater than 0
  63. assert (binary_number[i] > -1);
  64. }
  65. }
  66. }
  67. /* Thus function basically takes a number, finds it's highest power-of-2 factor
  68. ** adds that to the global binary_number, and loops again,
  69. ** This is not a functional function. It changes the value of n.
  70. ** And it changes the global variable binary_number
  71. */
  72. static void calc_b_number (int n)
  73. {
  74. extern short binary_number [];
  75. short highest_power_of_2;
  76. while (n > 0)
  77. {
  78. highest_power_of_2 = return_highest_power_of_2 (n);
  79. /* add the highest power of n to binary_number */
  80. special_plus (highest_power_of_2);
  81. /* set n to difference of n and the highest power of n, and loop again */
  82. n = n - pow(2, highest_power_of_2);
  83. }
  84. assert (n >= 0);
  85. }
  86. void print_b_number (int n)
  87. {
  88. extern short binary_number [];
  89. bool print = false;
  90. calc_b_number (n);
  91. for (int i = 0; i < MAX_NUMBER; i++)
  92. {
  93. if (binary_number[i])
  94. {
  95. print = true;
  96. }
  97. if (print)
  98. printf ("%d", binary_number[i]);
  99. }
  100. printf("\n");
  101. /* set the binary number to 0 again,
  102. so we can call print_binary once more */
  103. for (int j = 0; j < MAX_NUMBER; j++)
  104. {
  105. binary_number[j] = 0;
  106. }
  107. }
  108. int main ()
  109. {
  110. print_b_number (1);
  111. print_b_number (2);
  112. print_b_number (3);
  113. print_b_number (4);
  114. print_b_number (8);
  115. print_b_number (13);
  116. print_b_number (16);
  117. //printf ("%d\n", return_highest_power_of_2 (32));
  118. //printf ("%3.0f\n", calc_b_number (2));
  119. //printf (" %d\n", (int) calc_b_number (2));
  120. return 0;
  121. }