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.

Printable View

- 09-18-2009Tom.b
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.

- 09-24-2009Tom.bAnother 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", );

}

2. What should this loop look like with division of 2? I didnt get a answer while googling it.

thanks for your help,

tom - 09-24-2009tabstop
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.

- 09-24-2009MK27
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 */

- 09-24-2009Tom.b
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? - 09-24-2009MK27
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;

}

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.