please Explain this program... The output displayed as fff0Code:#include<stdio.h> void main() { printf("%x",-1<<4); }
please Explain this program... The output displayed as fff0Code:#include<stdio.h> void main() { printf("%x",-1<<4); }
Last edited by ishwariamca; 09-04-2008 at 04:20 AM.
And what do you want us to explain? That is exactly the output I'd expect.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I cannot understand the concept clearly. please let me know in detail.
Yes, but I still don't know what you are actually asking for:
- how a negative number is represented,
- what a shift operator does,
- how printf display hexadecimal numbers
- something else that I haven't been able to guess
I could spend quite a bit of time explaining any of the first three, and still not hit what you are actually asking about.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd
I'm still confused as to what we should explain - how #include <stdio.h> works, why the output looks like it does, or something else.
I too am sure that the answer should fit in some box on an assignment sheet, but I also would like to have a distinct question that can be answered, rather than "explain this" and a bit of code - 'tis a bit vague!
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I'm not in school, so don't be wary of any assignment sheet answer. I read the post and wanted to know what is the purpose of a shift operator. This may be beyond where I'm at in learning C anyway, but I figured I would ask.
"The art of living is more like wrestling than dancing." - Marcus Aurelius
I reckon it's something along the lines of: Please explain what is happening, given this program and this output. So the answer is along the lines of:
"-1 is represented in binary as [blah], the bitshift then moves [blah], finally, the format specifier [blah]. Thence you get the above output."
I've blah'ed out all the interesting bits of the answer, ishwariamca, if there is a part of the program you don't understand, ask a specific question.
QuantumPete
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd
>> I read the post and wanted to know what is the purpose of a shift operator. This may be beyond where I'm at in learning C anyway, but I figured I would ask.
The applications are countless, this site has a pretty good tutorial on them: http://www.cprogramming.com/tutorial...operators.html
It uses a car park example.
Thank you.
"The art of living is more like wrestling than dancing." - Marcus Aurelius
The shift operator corresponds to a set of instructions that most processors have, which multiplies/divides by 2 to the power of N. (simple, old processors would only do shifts one bit at at time, so only multiply or divide by 2). Compared to regular divide/multiply operations on the processor, these are MUCH faster [and sometimes the only type of multiply/divide, so any other form of multiplication would have to be built on a basis of multiplying/dividing by 2 - a bit like we do long multiplication or division on paper, except we use multiply/divide by 10 to figure out the result step by step].
This is useful when working with the binary representation of numbers, or when for example extracting a number that occupies bits in the middle of a larger number.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Okay, thank you.
Last edited by Dogmasur; 09-04-2008 at 06:49 AM. Reason: Because I forgot how to write in proper English
"The art of living is more like wrestling than dancing." - Marcus Aurelius
I know someone probably answered, but this one calls for some visual aid:
(32-bit) Example:
Make sense?Code:-1 // our signed int version of a binary value 11111111111111111111111111111111 //in binary -1 < 4 // means that 4 bits are shifted left 11111111111111111111111111110000 // in binary
You should mention it's in 2s-complement
It makes more sense in signed magnitude, although that's not what happens...
Code:-1 // our signed int version of a binary value 10000000000000000000000000000001 //in binary (signed magnitude) -1 << 4 // means that 4 bits are shifted left 00000000000000000000000000010000 // in binary, note the value is NOT sign extended
Thank You for your explanation.