MATH.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // C source file with some math-related function definitions
  2. // TODOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOo
  3. // int_powerof() function
  4. // function to get the result of a given number to
  5. // the power of a given positive integer number.
  6. // Used mainly on the binary_to_decimal() and
  7. // get_str_weight() functions.
  8. //
  9. // exponent (byte_t) --> exponent number
  10. smax_t int_powerof(smax_t base, byte_t exponent)
  11. {
  12. smax_t result = 1;
  13. for (smax_t i = 1; i <= exponent; i++)
  14. result *= base;
  15. return result;
  16. }
  17. // char_array_to_binary() function
  18. // function that takes a character array containing the bytes of an
  19. // integer and converts each byte of it into its binary representation
  20. //
  21. // integer_size (byte_t) --> size of the integer to be converted in bytes
  22. // integer_bytes (byte_t *) --> character array pointer containing the bytes of the integer
  23. // integer_bits (byte_t *) --> character array pointer that will contain the bits calculated
  24. // by this function
  25. void char_array_to_binary(byte_t integer_size, byte_t * integer_bytes, byte_t * integer_bits)
  26. {
  27. // loop through integer_bytes array
  28. for (smax_t i = 0; i < integer_size; i++)
  29. {
  30. // get current byte value from integer_bytes array
  31. byte_t currrent_int_byte = integer_bytes[i];
  32. byte_t current_arr_pos = i * 8;
  33. // do 8 loops to get the 8 bits of cur_var_byte
  34. for (smax_t j = 0; j < 8; j++)
  35. {
  36. // binary representation is calculated backwards so to put it forwards
  37. // on the array I have to visit the array elements from right to left
  38. integer_bits[(current_arr_pos + 7) - j] = currrent_int_byte % 2;
  39. currrent_int_byte = currrent_int_byte / 2;
  40. }
  41. }
  42. }
  43. // binary_to_decimal() function
  44. // function used to convert a character array containing 0s
  45. // and 1s into its decimal representation (signed or unsigned)
  46. //
  47. // binary_array_length (byte_t) --> length of the array containing the binary number
  48. // binary_array (byte_t *) --> pointer to the array containing the binary number
  49. // is_signed (bool_t) --> boolean to specify if integer is signed or not
  50. smax_t binary_to_decimal(byte_t binary_array_length, byte_t * binary_array, bool_t is_signed)
  51. {
  52. smax_t result = 0;
  53. for (smax_t i = 0; i < binary_array_length; i++)
  54. {
  55. // skip first element on variable_bin_rep
  56. // as the number might be signed/unsigned
  57. if (i == 0)
  58. continue;
  59. if (binary_array[i] == 1)
  60. result += int_powerof(2, (binary_array_length - 1) - i);
  61. }
  62. // check is_signed and the first element on variable_bin_rep
  63. // and apply the respective operations to get the final number
  64. // and return result
  65. if (is_signed == false && binary_array[0] == 0)
  66. return result;
  67. else if (is_signed == false && binary_array[0] == 1)
  68. return result + int_powerof(2, binary_array_length - 1);
  69. else if (is_signed == true && binary_array[0] == 0)
  70. return result;
  71. else if (is_signed == true && binary_array[0] == 1)
  72. return (result + 1) * (-1);
  73. // if somehow none of the above conditions
  74. // are valid then return 0
  75. return 0;
  76. }