any hints to solving this?

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...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    32

    any hints to solving this?

    Convert (10.1101) binary to decimal

  2. #2
    Registered User
    Join Date
    Jul 2005
    Posts
    21
    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.
    Last edited by saeculum; 03-12-2009 at 08:53 AM.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,299
    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.
    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

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,672
    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.
    I used to be an adventurer like you... then I took an arrow to the knee.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,299
    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

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,022
    Quote Originally Posted by hk_mp5kpdw View Post
    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
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. JOB: C/C++ Developer with problem solving skills
    By VoltRecruiter in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 01-25-2006, 11:25 PM
  2. Can you give me some hints?
    By mag_chan in forum C Programming
    Replies: 6
    Last Post: 12-05-2005, 03:04 PM
  3. Can you give me some hints?
    By mag_chan in forum C++ Programming
    Replies: 1
    Last Post: 12-04-2005, 01:12 AM
  4. Math: solving outcomes...
    By Oktos in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 04-10-2003, 06:55 AM
  5. Hints required to complete simple programs
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 06-30-2002, 09:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21