Thread: Explain

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    11

    Explain

    Code:
    #include<stdio.h>
    void main()
    {
    printf("&#37;x",-1<<4);
    }
    please Explain this program... The output displayed as fff0
    Last edited by ishwariamca; 09-04-2008 at 04:20 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    11

    Doubt

    I cannot understand the concept clearly. please let me know in detail.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by ishwariamca View Post
    I cannot understand the concept clearly. please let me know in detail.
    Does this explanation need to be limited to a hundred words, so it fits into the answer box on your assignment sheet?

    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

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by QuantumPete View Post
    Does this explanation need to be limited to a hundred words, so it fits into the answer box on your assignment sheet?

    QuantumPete
    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.

  7. #7
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    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

  8. #8
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by matsp View Post
    rather than "explain this" and a bit of code - 'tis a bit vague!
    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

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    >> 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.

  10. #10
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    Thank you.
    "The art of living is more like wrestling than dancing." - Marcus Aurelius

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I know someone probably answered, but this one calls for some visual aid:

    (32-bit) Example:
    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
    Make sense?

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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

  13. #13
    Registered User
    Join Date
    Aug 2008
    Posts
    11
    Thankyou

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  2. Please Explain me few terms that i have listed in here.
    By chottachatri in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 08:20 AM
  3. Can someone explain to me what this code means
    By Shadow12345 in forum C++ Programming
    Replies: 3
    Last Post: 12-22-2002, 12:36 PM
  4. Replies: 4
    Last Post: 11-19-2002, 09:18 PM
  5. Can someone explain "extern" to me?
    By valar_king in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2001, 12:22 AM