Convert (10.1101) binary to decimal
This is a discussion on any hints to solving this? within the C Programming forums, part of the General Programming Boards category; Convert (10.1101) binary to decimal...
Convert (10.1101) binary to decimal
EDIT:Code:#include <stdio.h> #include <math.h> #include <string.h> int main( void ) { char *bitstring = "10.1101"; int decimalPoint = 0; /* Location of decimal */ int i = 0; /* Counter1 */ int j = 0; /* Counter2 */ double value = 0.00; /* Locate decimal point */ for( i = 0; i < strlen(bitstring); i++ ) { if( bitstring[i] == '.' ) { decimalPoint = i; break; } } /* Calculate exponent */ for( i += 1, j = 1; i < strlen(bitstring); i++, j++ ) { if( bitstring[i] == '1' ) { value += (double)(1.00 / (double)pow(2.00,(double)j)); } } /* Calculate Mantissa */ for( i = decimalPoint-1, j = 0; i >= 0; i--, j++ ) { if( bitstring[i] == '1' ) { value += (double)pow(2.00,(double)j); } } printf("%s = %f\n", bitstring, value); return 0; }
Sorry I'll explain it a little..
The bitstring is a series of 1's and 0's, the delimiter between the mantissa and exponent being the decimal point.
Step 1: You need to find the location of the decimal point, and you also need to know the location of the LSB of the mantiisa - so you do this.
Step 2: You've found the decimal, set i to decimalPoint and decrement i to point to the LSB of the mantissa and begin calculating powers. You start with i = 0 and (2^i)*bit and as you move towards the MSB you increment t i by 1. So you have the summation of( 2^i)*bit where i = (decimalPoint-1) to 0.
Step 3: Now you need to exponent side of the bitstring, so you point to the next bit after the decimal. You start with i = 1 and (1.00 / (2^i))*bit to get the actual decimal value. As you move towards the LSB of the mantissa you need to increment i by 1. So you have the summation of (1.00 / (2^i)*bit, where i = 0 to strlen(bitstring)-decimalPoint.
The summation of the value obtained from step 2 and the value obtained from step 3 is your decimal value.
Last edited by saeculum; 03-12-2009 at 09:53 AM.
This converts binary numbers too:
It doesn't deal with decimals tho, and you can't convert the right side the same way I wouldn't think.Code:#include <stdio.h> #include <string.h> int main (int argc, char *argv[]) { int i, bit=1, result=0, len=strlen(argv[1]); for (i=len-1;i>=0;i--) { if (argv[1][i]!='1') { bit*=2; continue; } result|=bit; bit*=2; } printf("Binary %s is decimal %d\n",argv[1],result); return 0; }
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Values to the left of the decimal represent powers of 2 (starting at 2 raised to the 0th power). Values to the right of the decimal represent powers of ½ (starting at ½ raised to the 1st power).
Example: 1010.0101
The values to the left of the decimal are 1010. These represent the value (1 * 2 ^ 3 + 0 * 2 ^ 2 + 1 * 2 ^ 1 + 0 * 2 ^ 0). Simplifying this you get (1 * 8 + 0 * 4 + 1 * 2 + 0 * 1) which is (8 + 2) or 10 (decimal).
The values to the right of the decimal are 0101. These represent the value (0 * ½ ^ 1 + 1 * ½ ^ 2 + 0 * ½ ^ 3 + 1 * ½ ^ 4). Simplifying this you get ( 0 * 0.5 + 1 * 0.25 + 0 * 0.125 + 1 * 0.0625) which is (0.25 + 0.0625) or 0.3125.
Combining these two you get 10.3125 as the final answer.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
Thanks hk_mp5kpdw for that. So I think this is an optimized solution:
Code:#include <stdio.h> #include <string.h> int getdot (char *string, int len) { int i; for (i=0;i<len;i++) if (string[i]=='.') return i; return len; /* no decimal places */ } int main (int argc, char *argv[]) { float result,right=0.0f, tmp; int i, ii, left=0, len=strlen(argv[1]), dot=getdot(argv[1],len), bit=1; /* integer */ for (i=dot-1;i>=0;i--) { if (argv[1][i]!='1') { bit*=2; continue; } left|=bit; bit*=2; } result=(float)left; /* integer fraction */ for (i=dot+1;i<len;i++) { if (argv[1][i]!='1') continue; tmp=0.5f; for (ii=0;ii<i-dot-1;ii++) tmp/=2; right+=tmp; } result+=right; printf("Binary %s is decimal %f\n",argv[1],result); return 0; } Example output: [root~/C] ./a.out 1010.0101 Binary 1010.0101 is decimal 10.312500
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"