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.
This is a discussion on Problem with my code within the C Programming forums, part of the General Programming Boards category; I got the zero before the value with "%08x". Youre right number should be printed as unsigned int to get ...
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.
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:
1. Do i need to save all %1d in new variable so bit1, bit2 ... or just in 1 called bit?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
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.
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.
ps. you should start a new thread for a new question, maybe one of the mod's will split this offCode:int x = 11; /* decimal */ int y = 0xb; /* same thing, hexadecimal */
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
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 03:01 PM.
This is what is meant by loop:
You must compile this "-lm" or however your compiler links to the math library.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.
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