# any hints to solving this?

• 03-12-2009
sjcc
any hints to solving this?
Convert (10.1101) binary to decimal
• 03-12-2009
saeculum
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; }```
EDIT:
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.
• 03-12-2009
MK27
This converts binary numbers too:
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; }```
It doesn't deal with decimals tho, and you can't convert the right side the same way I wouldn't think.
• 03-12-2009
hk_mp5kpdw
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.
• 03-12-2009
MK27
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```
• 03-12-2009
iMalc
Quote:

Originally Posted by hk_mp5kpdw
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).

Or you could basically take it that every digit before or after the decimal point is a power of two. Those that are to the right of the decimal point are simply to a negative power of two.
0.1 ==> 2^-1
0.01 ==> 2^-2