Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat/fix/docs]: Improved on conversion code , length variable , asserts added #840

Open
wants to merge 45 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
67eeefe
Added asserts , number length check
lazy-dude Jul 8, 2021
0008c67
Remove extra empty lines
lazy-dude Jul 8, 2021
7185941
Auto indent
lazy-dude Jul 8, 2021
6e5e561
fix: corrected shown hex number.
lazy-dude Jul 8, 2021
905b80c
Changed to recommended typical structure
lazy-dude Jul 10, 2021
e771021
docs: Added some doxygen doc.
lazy-dude Jul 14, 2021
157f6d1
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
bbb6024
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
82e1f41
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
1e4c396
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
166dc0b
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
57bcffa
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
129f4b0
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
8526600
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
61bed00
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
4d91c9d
Update conversions/binary_to_decimal.c
lazy-dude Jul 17, 2021
8defb93
Followed the two suggestions on code. Explain includes, intmax_t->uin…
lazy-dude Jul 17, 2021
6397733
Update conversions/binary_to_decimal.c
lazy-dude Jul 18, 2021
45e4eff
Update conversions/binary_to_decimal.c
lazy-dude Jul 18, 2021
6553640
Update conversions/binary_to_decimal.c
lazy-dude Jul 18, 2021
09df4a4
Update conversions/binary_to_decimal.c
lazy-dude Jul 18, 2021
63cf38c
Changes of some types.
lazy-dude Jul 18, 2021
e078df2
Update conversions/binary_to_decimal.c
lazy-dude Jul 21, 2021
0b199fb
Update conversions/binary_to_decimal.c
lazy-dude Jul 21, 2021
f66cd1b
Update conversions/binary_to_decimal.c
lazy-dude Jul 21, 2021
fd82d9e
Update conversions/binary_to_decimal.c
lazy-dude Jul 21, 2021
ebf83bb
Update conversions/binary_to_decimal.c
lazy-dude Jul 21, 2021
63a9a16
Update conversions/binary_to_decimal.c
lazy-dude Jul 21, 2021
a2ca660
Update conversions/binary_to_decimal.c
lazy-dude Jul 21, 2021
81ab47c
Considered some suggestions
lazy-dude Jul 21, 2021
0f96de5
Update conversions/binary_to_decimal.c
lazy-dude Jul 22, 2021
254cabf
Update conversions/binary_to_decimal.c
lazy-dude Jul 22, 2021
8c1b548
Update conversions/binary_to_decimal.c
lazy-dude Jul 22, 2021
8632490
Added link for explaination.
lazy-dude Jul 22, 2021
8fa92c5
Update conversions/binary_to_decimal.c
lazy-dude Jul 23, 2021
4ab5a25
Update conversions/binary_to_decimal.c
lazy-dude Jul 23, 2021
da73878
Update conversions/binary_to_decimal.c
lazy-dude Jul 23, 2021
71c17ee
return -> returns
lazy-dude Jul 23, 2021
c3433f7
Update conversions/binary_to_decimal.c
lazy-dude Jul 26, 2021
53e719b
Update conversions/binary_to_decimal.c
lazy-dude Jul 26, 2021
af4b346
Update conversions/binary_to_decimal.c
lazy-dude Jul 26, 2021
aa8e55b
Update conversions/binary_to_decimal.c
lazy-dude Jul 26, 2021
0d61a4a
Update conversions/binary_to_decimal.c
lazy-dude Jul 27, 2021
82fb3ae
Update conversions/binary_to_decimal.c
lazy-dude Jul 27, 2021
9612325
Added description
lazy-dude Jul 27, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 43 additions & 13 deletions conversions/binary_to_decimal.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,54 @@
/**
* Modified 07/12/2017, Kyler Smith
*
*/

#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

bool is_binary(intmax_t num);
int num_len(intmax_t num);

int main()
int main(void)
lazy-dude marked this conversation as resolved.
Show resolved Hide resolved
{
int remainder, number = 0, decimal_number = 0, temp = 1;
printf("\n Enter any binary number= ");
scanf("%d", &number);
intmax_t remainder, number = 0, decimal_number = 0, temp = 1;

int length = num_len(INTMAX_MAX) - 1;
printf("\n Enter any binary number , max %d digits: ", length);
scanf("%jd", &number);
assert(num_len(number) <= length);
assert(is_binary(number));

// Iterate over the number until the end.
while (number > 0)
{
while (number > 0) {
remainder = number % 10;
number = number / 10;
decimal_number += remainder * temp;
temp = temp * 2; // used as power of 2
temp = temp * 2; // used as power of 2
}

printf("%jd\n", decimal_number);
return 0;
}

bool is_binary(intmax_t num)
{
int remainder = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using uint64_t for non-negative values (or their appropriate size: uint32_t, uint16_t, uint8_t) or int64_t for negative values. Requires adding the inttypes.h library. Check other parts of the code (reference). 🙂

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uintmax_t has the biggest length for machine running the code. I would prefer it over uint64_t which has a fixed size.


while (num > 0) {
remainder = num % 10;
if (remainder == 0 || remainder == 1) {
num /= 10;
continue;
} else
return false;
}
return true;
}

printf("%d\n", decimal_number);
int num_len(intmax_t num)
{
int i;
for (i = 0; num > 0; i++) {
num /= 10;
}
return i;
}