Explain

This is a discussion on Explain within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> void main() { printf("&#37;x",-1<<4); } please Explain this program... The output displayed as fff0...

  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 05: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
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Dogmasur View Post
    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 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.

  12. #12
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    Okay, thank you.
    Last edited by Dogmasur; 09-04-2008 at 07:49 AM. Reason: Because I forgot how to write in proper English
    "The art of living is more like wrestling than dancing." - Marcus Aurelius

  13. #13
    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?

  14. #14
    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

  15. #15
    Registered User
    Join Date
    Aug 2008
    Posts
    11
    Thank You for your explanation.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-23-2008, 02: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, 01:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21