-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathbinary_to_decimal.c
More file actions
104 lines (92 loc) · 2.38 KB
/
binary_to_decimal.c
File metadata and controls
104 lines (92 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* @file
* @brief Converts a binary number to a decimal one.
* @details
* A binary number is an input, it is checked to be binary
* then, the number is converted to a decimal number.
* [Binary to decimal](https://byjus.com/binary-to-decimal-formula/)
* @author [Kyler Smith](https://github.com/KylerSmith)
* @author [lazy-dude](https://github.com/lazy-dude)
*/
#include <assert.h> /// for assert
#include <stdbool.h> /// for bool
#include <stdint.h> /// for uintmax_t
#include <stdio.h> /// for IO operations
/**
* @brief checks whether the number is binary
* @param num to be checked if it has binary representation
* @returns true if the number IS binary
* @returns false if the number is NOT binary
*/
bool is_binary(uintmax_t num)
{
unsigned remainder = 0;
while (num > 0) {
remainder = num % 10;
if (remainder == 0 || remainder == 1) {
num /= 10;
continue;
} else
return false;
}
return true;
}
/**
* @brief finds the length of an `uintmax_t` number
* @param num whose length to be computed
* @return the length of the number
*/
uint16_t num_len(uintmax_t num)
{
uint16_t len = 0;
for (len = 0; num > 0; len++) {
num /= 10;
}
return len;
}
/**
* @brief The `binary_decimal` function does the actual job of conversion
* @param the number binary to be converted
* @return decimal_number decimal representation of a binary number
*/
uintmax_t binary_decimal(uintmax_t number)
{
uint8_t remainder;
uintmax_t decimal_number = 0;
uint32_t temp = 1;
uint16_t length = num_len(UINTMAX_MAX) - 1;
assert(num_len(number) <= length);
assert(is_binary(number));
// Iterate over the number until the end.
while (number > 0) {
remainder = number % 10;
number = number / 10;
decimal_number += remainder * temp;
temp = temp * 2; // used as power of 2
}
return decimal_number;
}
/**
* @brief Self-test implementations
* @returns void
*/
static void test()
{
assert(binary_decimal(0)==0);
assert(binary_decimal(1)==1);
assert(binary_decimal(1110001)==113);
assert(binary_decimal(11111111)==255);
assert(binary_decimal(10000000000)==1024);
assert(binary_decimal(1001110100000100)==40196);
printf("All tests have passed!\n");
}
/**
* @brief main function
* @param void
* @returns 0 on exit
*/
int main()
{
test(); // run self-test implementations
return 0;
}