123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- /*
- This program prints a binary representation of a decimal number.
- To do this, it create an array of ints. So to store the binary number of 12: 1100,
- the array of ints looks like
- {0,0,0,....,1,1,0,0}
-
- It is probably using a fairly poor algorithm of converting decimals to ints, but oh well.
- It finds the highest power of 2 less than the number, stores that
- in the array of ints, at the end, then subtracts to find the next power of 2...
- The algorithm is not funtional at all. I
-
- 12 --> binary
- (8 --> binary) + ((12 - 8) --> binary)
- 2^3
- {0,0,0...1,0,0,0} + (4 --> binary) + ((4 - 4) --> binary)
- 2^2
- {0,0,0...0,1,0,0} + 0
- {0,0,0...1,0,0,0}
- + {0,0,0...0,1,0,0}
- -----------------
- {0,0,0...1,1,0,0}
-
- */
- #include <math.h>
- #include <assert.h>
- #include <stdio.h>
- #include <stdbool.h>
- #define MAX_NUMBER 128
- short binary_number [MAX_NUMBER];
- /* This function takes an int and prints its binary representation */
- static int return_highest_power_of_2 (int i)
- {
- /* find the highest power of 2 <= i */
- short int n = 0;
- for (; pow(2 , n) <= i; ++n)
- {
- }
- return --n;
- }
- /* This function adds a power of 2 (n), to the binary number array.
- * ie:
- binary_number = {0,0,0...1,0,1,0}
- n = 2
- {0,0,0...1,0,1,0}
- + 1 0 0
- ----------------
- return -> {0,0,0...1,1,1,0}
- */
- static void special_plus (int n)
- {
- extern short binary_number [];
- for (int i = MAX_NUMBER - n - 1; i >= 0; i--) //this changes the value of N. That's a problem! FIXME
- {
- binary_number[i]++;
- if (binary_number[i] == 1)
- {
- break;
- }
- else if (binary_number[i] > 1)
- {
- binary_number[i] = 1;
- }
- else
- {
- //binary_number[i] should be greater than 0
- assert (binary_number[i] > -1);
- }
- }
- }
- /* Thus function basically takes a number, finds it's highest power-of-2 factor
- ** adds that to the global binary_number, and loops again,
- ** This is not a functional function. It changes the value of n.
- ** And it changes the global variable binary_number
- */
- static void calc_b_number (int n)
- {
- extern short binary_number [];
- short highest_power_of_2;
- while (n > 0)
- {
- highest_power_of_2 = return_highest_power_of_2 (n);
- /* add the highest power of n to binary_number */
- special_plus (highest_power_of_2);
- /* set n to difference of n and the highest power of n, and loop again */
- n = n - pow(2, highest_power_of_2);
- }
- assert (n >= 0);
- }
- void print_b_number (int n)
- {
- extern short binary_number [];
- bool print = false;
- calc_b_number (n);
- for (int i = 0; i < MAX_NUMBER; i++)
- {
- if (binary_number[i])
- {
- print = true;
- }
- if (print)
- printf ("%d", binary_number[i]);
- }
- printf("\n");
- /* set the binary number to 0 again,
- so we can call print_binary once more */
- for (int j = 0; j < MAX_NUMBER; j++)
- {
- binary_number[j] = 0;
- }
- }
- int main ()
- {
- print_b_number (1);
- print_b_number (2);
- print_b_number (3);
- print_b_number (4);
- print_b_number (8);
- print_b_number (13);
- print_b_number (16);
- //printf ("%d\n", return_highest_power_of_2 (32));
- //printf ("%3.0f\n", calc_b_number (2));
- //printf (" %d\n", (int) calc_b_number (2));
- return 0;
- }
|