From ec994a46df4a88f6e7b592827fe89b2f2a7ed221 Mon Sep 17 00:00:00 2001 From: Hyder Date: Thu, 14 Aug 2025 20:15:29 +0300 Subject: Solved the binary to decimal converter exercise --- BTD/28bitToDecimal.c | 31 +++++++++++++++++++++++++++++++ README | 3 ++- 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 BTD/28bitToDecimal.c diff --git a/BTD/28bitToDecimal.c b/BTD/28bitToDecimal.c new file mode 100644 index 0000000..ab60959 --- /dev/null +++ b/BTD/28bitToDecimal.c @@ -0,0 +1,31 @@ +# include + +# define bitvalue_length 27 + +unsigned long int powerOFtwo(int value) { + unsigned long int result = 1; + for (int i = value;i > 0;i--) { + result *= 2; + } + return result; +} + +int main () { + printf("28-bit binary number converter to decimal:\n\n"); + char bitvalue[28]; + scanf("%s", bitvalue); + unsigned long int decimalValue = 0; + for (int i = bitvalue_length;i >= 0;i--) { + if (bitvalue[i] == '0') { + decimalValue += 0; + } + else if (bitvalue[i] == '1') { + unsigned long int tmp = powerOFtwo(bitvalue_length - i); + decimalValue += tmp; + } + } + + printf("%lu\n" , decimalValue); + + return 0; +} \ No newline at end of file diff --git a/README b/README index 09b1f54..debff5d 100644 --- a/README +++ b/README @@ -9,4 +9,5 @@ CONTENTS - The greatest common divisor exercise solution, in ROOT/GCD/GCD.c - Hexadecimal to integer converter solution, in ROOT/HTOI/HTOI.c -- Vowels and consonants counter of a string, in ROOT/VACC/VowelsAndConsonantCounter.c \ No newline at end of file +- Vowels and consonants counter of a string, in ROOT/VACC/VowelsAndConsonantCounter.c +- Binary(28-bit) to decimal converter, in ROOT/BTD/28bitToDecimal.c \ No newline at end of file -- cgit v1.2.3