Thread: Problem with my code

  1. #16
    Registered User
    Join Date
    Sep 2009
    Location
    Netherlands
    Posts
    17
    I got the zero before the value with "%08x". Youre right number should be printed as unsigned int to get 4294967295 as in the manual. Thanks for your help both! I really appreciate it.

  2. #17
    Registered User
    Join Date
    Sep 2009
    Location
    Netherlands
    Posts
    17

    Another week, another problem

    Hi,

    I got a new (for you easy) task. I should write a short code for two's complement to decimal conversion. I shoud use scanf("%1d", &bit) to get the number. Then use a for loop. Build a efficient representation that does not calculate 2^n instead use repeated division by 2.

    This is what i have:
    Code:
    #include <stdio.h>
    
    void main (void)
    {
    	int bit;
    	int decimal;
    
    
    	printf("Enter a 16 bit binary two's complement number:\n");
    	scanf("%1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d",
    			&bit);
    	for (bit;bit!=0; bit++)
    
    	
    	if(bit1==1)
    	{
    		decimal = number * -1;
    	}
    	else
    	{
    		decimal = number;
    	}
    
    	printf("The 2-complement decimal representation is: \n", );
    
    
    
    }
    1. Do i need to save all %1d in new variable so bit1, bit2 ... or just in 1 called bit?
    2. What should this loop look like with division of 2? I didnt get a answer while googling it.

    thanks for your help,
    tom

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What the problem seems to suggest is that you need your scanf in a loop -- read in a bit, and then use that bit to modify your current guess as to what the number is (if it's a zero, do this; if it's a one, do that). You should not (according to the problem you provided) be trying to read in 16 bits with one scanf statement.

  4. #19
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Tom.b View Post
    1. Do i need to save all %1d in new variable so bit1, bit2 ... or just in 1 called bit?
    Afraid so. I would actually take this as a string and then convert it to a number, since there is no easy way to assign an int a binary value, the same way you can assign it a decimal or hex value.
    Code:
    int x = 11; /* decimal */
    int y = 0xb; /* same thing, hexadecimal */
    ps. you should start a new thread for a new question, maybe one of the mod's will split this off
    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

  5. #20
    Registered User
    Join Date
    Sep 2009
    Location
    Netherlands
    Posts
    17
    This is what the manual actually says:
    Write a C program that performs the conversion of a 16 bit two's complement binary number into its corresponding decimal value. Read the bits digit-by-digit using scanf("%1d", &bit). Every time you use this scanf you read in 1 digit so you should use scanf multiple times. Use a for loop to implement the iteration over i in the above formula (sum formula of 2^i). Build an efficient representation that does not calculate 2^n. Instead, use e.g. repeated (and efficient implemented) division by 2.
    Hint: 2^x = 1 << x and x/2 = x >> 1

    I think i should read in all bits. Then put in a for loop that searches for 0 (from the second bit). If it finds a 0 on lets say the 4 bit it should divide 32768 (2^15) by 4 (2^2). But this does work only 1 time???
    I think it is not allowed to read it in like a string if i read the manual. But if you think it is can you explain a bit better how i should do this?
    Last edited by Tom.b; 09-24-2009 at 02:01 PM.

  6. #21
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    This is what is meant by loop:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void) {
    	int bit, val = 0, i;
    	printf("Enter an 8-bit binary number: ");
    	for (i=0;i<8;i++) {
    		scanf("%1d",&bit);
    		if (bit) val += pow(2,(7-i));
    	}
    	printf("Decimal: %d\n", val);
    	return 0;
    }
    You must compile this "-lm" or however your compiler links to the math library.

    Notice this is for an unsigned value, and it does not use the method you are supposed to, but the loop, reusing "bit" is there.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem
    By sybariticak47 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2006, 11:50 AM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM