diff options
Diffstat (limited to 'BTD')
| -rw-r--r-- | BTD/28bitToDecimal.c | 31 |
1 files changed, 31 insertions, 0 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 |
