diff options
| author | Hyder <hyder@hyderhadi.xyz> | 2025-08-14 20:15:29 +0300 |
|---|---|---|
| committer | Hyder <hyder@hyderhadi.xyz> | 2025-08-14 20:15:29 +0300 |
| commit | ec994a46df4a88f6e7b592827fe89b2f2a7ed221 (patch) | |
| tree | 0bb429c4fc376bb564722295ed8dcc96dba7388b | |
| parent | fc946f55c53bd287e8ca5ebaa1a136b5ae4ac725 (diff) | |
Solved the binary to decimal converter exercise
| -rw-r--r-- | BTD/28bitToDecimal.c | 31 | ||||
| -rw-r--r-- | README | 3 |
2 files changed, 33 insertions, 1 deletions
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 <stdio.h> + +# 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 @@ -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 |
