-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
[feat/fix/docs]: Improved on conversion code , length variable , asserts added #840
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
Open
lazy-dude
wants to merge
45
commits into
TheAlgorithms:master
Choose a base branch
from
lazy-dude:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
67eeefe
Added asserts , number length check
lazy-dude 0008c67
Remove extra empty lines
lazy-dude 7185941
Auto indent
lazy-dude 6e5e561
fix: corrected shown hex number.
lazy-dude 905b80c
Changed to recommended typical structure
lazy-dude e771021
docs: Added some doxygen doc.
lazy-dude 157f6d1
Update conversions/binary_to_decimal.c
lazy-dude bbb6024
Update conversions/binary_to_decimal.c
lazy-dude 82e1f41
Update conversions/binary_to_decimal.c
lazy-dude 1e4c396
Update conversions/binary_to_decimal.c
lazy-dude 166dc0b
Update conversions/binary_to_decimal.c
lazy-dude 57bcffa
Update conversions/binary_to_decimal.c
lazy-dude 129f4b0
Update conversions/binary_to_decimal.c
lazy-dude 8526600
Update conversions/binary_to_decimal.c
lazy-dude 61bed00
Update conversions/binary_to_decimal.c
lazy-dude 4d91c9d
Update conversions/binary_to_decimal.c
lazy-dude 8defb93
Followed the two suggestions on code. Explain includes, intmax_t->uin…
lazy-dude 6397733
Update conversions/binary_to_decimal.c
lazy-dude 45e4eff
Update conversions/binary_to_decimal.c
lazy-dude 6553640
Update conversions/binary_to_decimal.c
lazy-dude 09df4a4
Update conversions/binary_to_decimal.c
lazy-dude 63cf38c
Changes of some types.
lazy-dude e078df2
Update conversions/binary_to_decimal.c
lazy-dude 0b199fb
Update conversions/binary_to_decimal.c
lazy-dude f66cd1b
Update conversions/binary_to_decimal.c
lazy-dude fd82d9e
Update conversions/binary_to_decimal.c
lazy-dude ebf83bb
Update conversions/binary_to_decimal.c
lazy-dude 63a9a16
Update conversions/binary_to_decimal.c
lazy-dude a2ca660
Update conversions/binary_to_decimal.c
lazy-dude 81ab47c
Considered some suggestions
lazy-dude 0f96de5
Update conversions/binary_to_decimal.c
lazy-dude 254cabf
Update conversions/binary_to_decimal.c
lazy-dude 8c1b548
Update conversions/binary_to_decimal.c
lazy-dude 8632490
Added link for explaination.
lazy-dude 8fa92c5
Update conversions/binary_to_decimal.c
lazy-dude 4ab5a25
Update conversions/binary_to_decimal.c
lazy-dude da73878
Update conversions/binary_to_decimal.c
lazy-dude 71c17ee
return -> returns
lazy-dude c3433f7
Update conversions/binary_to_decimal.c
lazy-dude 53e719b
Update conversions/binary_to_decimal.c
lazy-dude af4b346
Update conversions/binary_to_decimal.c
lazy-dude aa8e55b
Update conversions/binary_to_decimal.c
lazy-dude 0d61a4a
Update conversions/binary_to_decimal.c
lazy-dude 82fb3ae
Update conversions/binary_to_decimal.c
lazy-dude 9612325
Added description
lazy-dude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,104 @@ | ||
/** | ||
* @file | ||
* @brief Converts a [binary number to a decimal](https://byjus.com/binary-to-decimal-formula/) number. | ||
* @details | ||
* A binary number is an input that is checked to be binary | ||
* then, the number is converted to a decimal number. | ||
* @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 | ||
|
||
/** | ||
* Modified 07/12/2017, Kyler Smith | ||
* | ||
* @brief checks whether the number is binary | ||
* @param num the number 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; | ||
|
||
#include <stdio.h> | ||
while (num > 0) { | ||
remainder = num % 10; | ||
// make sure each digit is 0 or 1 | ||
if (remainder == 0 || remainder == 1) { | ||
num /= 10; | ||
continue; | ||
} else | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
int main() | ||
/** | ||
* @brief finds the length of an `uintmax_t` number | ||
* @param num whose length to be computed | ||
* @returns the length of the number | ||
*/ | ||
uint16_t num_len(uintmax_t num) | ||
{ | ||
int remainder, number = 0, decimal_number = 0, temp = 1; | ||
printf("\n Enter any binary number= "); | ||
scanf("%d", &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 | ||
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 number the number binary to be converted | ||
* @returns 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; | ||
|
||
printf("%d\n", decimal_number); | ||
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"); | ||
} | ||
|
||
Panquesito7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* @brief main function | ||
* @param void | ||
* @returns 0 on exit | ||
*/ | ||
int main() | ||
{ | ||
test(); // run self-test implementations | ||
return 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.