aboutsummaryrefslogtreecommitdiff
path: root/BTD/28bitToDecimal.c
diff options
context:
space:
mode:
authorHyder <hyder@hyderhadi.xyz>2025-08-14 20:15:29 +0300
committerHyder <hyder@hyderhadi.xyz>2025-08-14 20:15:29 +0300
commitec994a46df4a88f6e7b592827fe89b2f2a7ed221 (patch)
tree0bb429c4fc376bb564722295ed8dcc96dba7388b /BTD/28bitToDecimal.c
parentfc946f55c53bd287e8ca5ebaa1a136b5ae4ac725 (diff)
Solved the binary to decimal converter exercise
Diffstat (limited to 'BTD/28bitToDecimal.c')
-rw-r--r--BTD/28bitToDecimal.c31
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