Printing memory from variable, is something wrong?

This is a discussion on Printing memory from variable, is something wrong? within the C Programming forums, part of the General Programming Boards category; Hey, I'm trying to print out the memory of a variable as binary. However I'm missing the signed bit for ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485

    Printing memory from variable, is something wrong?

    Hey, I'm trying to print out the memory of a variable as binary. However I'm missing the signed bit for some reason. Can some one spot the problem, or is it something I'm missing about the signed bit lol.

    Code:
    #include <stdio.h>
    
    int main()
    {
    	float test;
    	int *memdump, i;
    
    	printf("Enter value: ");
    	scanf("%f", &test);
    	printf("Entered:     %.0f\n", test);
    
    	memdump = (int*)&test;
    	
    	printf("S EXP      MANTISSA\n");
    	int binarray[32];
    	for(i = 0; i < 32; i++){
    		if(*memdump%2 == 0)
    			binarray[31-i] = 0;
    		else
    			binarray[31-i] = 1;
    		
    		*memdump -= (*memdump%2);
    		*memdump /= 2;
    	}
    	for(i = 0; i < 32; i++){
    		printf("%d", binarray[i]);
    		if(i == 0)
    			printf(" ");
    		if(i == 8)
    			printf(" (1)");
    	}
    	printf("\n–––––––––––––––––––––––––––––––––––––\n");
    
    	return 0;
    }
    Just to clarify, the signed bit is always zero.
    Last edited by Subsonics; 08-27-2009 at 03:12 PM. Reason: Added info

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Use unsigned int instead of signed int. You get to 1 or -1 divided by 2, and both answers yield 0.
    Last edited by King Mir; 08-27-2009 at 03:18 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Aah, thank you. I will try that. :-)

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Confirmed, it works! Thanks again.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    A few other points:
    And you don't need to subtract (*memdump%2). that will get truncated when rounding anyway.
    Consider using binarray[31-i] =*memdump%2.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Yes, that right, I did not think of that. It will make it more compact too. :-)

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    Also, keep in mind that floats and ints aren't guaranteed to be the same size. Here's a fairly standard approach that works with any type of data:

    Code:
    #include <stdio.h>
    #include <limits.h>
    
    char* memory_to_binary_text_lsb_to_msb_order( void* dat, size_t siz, char* buf, size_t len )
    {
        unsigned char
            msk,
            * seq = dat,
            * fin = seq + siz,
            * ptr = buf;
        if( siz == 0 || ( siz * CHAR_BIT + siz / CHAR_BIT ) >= len )
            return NULL;
        for( ;; )
        {
            for( msk = 0x1; msk; msk <<= 1 )
                *ptr++ = ( *seq & msk ) ? '1' : '0';
            if( ++seq == fin )
                break;
            *ptr++ = ' ';
        }
        *ptr = 0;
        return buf;        
    }
    
    int main( void )
    {
        double
            val = exp( 1.0 );
        char
            buf[ 128 ];
        puts
        ( 
            memory_to_binary_text_lsb_to_msb_order
            ( 
                &val, 
                sizeof( val ), 
                buf, 
                sizeof( buf ) 
            ) 
        );
        return 0;
    }
    Last edited by Sebastiani; 08-27-2009 at 04:22 PM. Reason: added sanity check for 'siz'

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Hey, Sebastiani, thanks for the tip. I will digest, and learn. Had to leave the terminal for some sleep, lol.

  9. #9
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    I was having a doubt in the program above. When we do

    memdump = (int*)&test;

    it is putting the address of test into memdump. But when i was trying to execute the program and using it in debug mode memdump has the address of test say 0x0012ff60 and when i tried to figure out the value of *memdump i thought it should be 4 but the value is something very large 1082130432. I mean i input 4 into test so when i do *memdump if i am not wrong it implies value at address contained in memdump so shouldnt it be 4.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    If you input 4, the floating point value should be 4. But since your reading the float as an int, the number is different. Still the program in the first post (preferably fixed) will generate the number S=0 E=10 M=0.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by roaan View Post
    I was having a doubt in the program above. When we do

    memdump = (int*)&test;

    it is putting the address of test into memdump. But when i was trying to execute the program and using it in debug mode memdump has the address of test say 0x0012ff60 and when i tried to figure out the value of *memdump i thought it should be 4 but the value is something very large 1082130432. I mean i input 4 into test so when i do *memdump if i am not wrong it implies value at address contained in memdump so shouldnt it be 4.
    That's true for an int, but the purpose for me was to be able to inspect how a floating point variable is represented internally.

  12. #12
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    i thought it should be 4 but the value is something very large 1082130432.
    Do you mean to say the float 4 is stored as this large number when stored in an integer variable.

  13. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by roaan View Post
    i thought it should be 4 but the value is something very large 1082130432.

    Do you mean to say the float 4 is stored as this large number when stored in an integer variable.
    Yes, in a float you get the number represented as , signed bit, then a eight bit exponent, then a 23bit mantissa. If you print this as an int then it will be interpreted as a large number since an int have no idea about floating point representation.

  14. #14
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by Subsonics View Post
    Yes, in a float you get the number represented as , signed bit, then a eight bit exponent, then a 23bit mantissa. If you print this as an int then it will be interpreted as a large number since an int have no idea about floating point representation.
    Yes i did read about the representation of floats. But then just will there be no overflow problem with the integers if we are representing such a large value.

  15. #15
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by roaan View Post
    Yes i did read about the representation of floats. But then just will there be no overflow problem with the integers if we are representing such a large value.
    Both the int and the float are 32bits on my machine, so there should be no problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. changing the address of a pointer question..
    By transgalactic2 in forum C Programming
    Replies: 42
    Last Post: 10-16-2008, 09:20 AM
  2. declared extra variable, is it wrong?
    By Lincoln_Poh in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2008, 07:48 AM
  3. Replies: 18
    Last Post: 12-31-2005, 12:56 PM
  4. Linked Lists
    By bigblack in forum C++ Programming
    Replies: 7
    Last Post: 02-05-2002, 04:21 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 01:26 PM

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