Thread: overflow...?

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

    overflow...?

    Code:
    int a= INT_MIN;
        a = -a;
        printf("%d", a);
    a still has a negative value! Why?

    It also mentions an overflow... how come?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Check the value of INT_MAX. Chances are you are dealing with a two's complement representation, so -INT_MIN cannot be stored in an int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by Tool View Post
    Code:
    int a= INT_MIN;
        a = -a;
        printf("%d", a);
    a still has a negative value! Why?

    It also mentions an overflow... how come?
    Let me explain it with a 16bit arithmetic. The value of INT_MIN for it will be -32768 and INT_MAX will be 32767. Thus when you negate INT_MIN you get 32768 but this can't be represented in it coz the maximum value of INT is 32767, thus it'll go to the other side and print the corresponding value which in this case is -32768(the same as INT_MIN). Would it had been 32769 it would have printed -32767 and so on.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #4
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Whats a two's complement representation?

    Yes i can print INT_MIN value. If i try printing -(INT_MIN) it's same as INT_MIN instead of being positive.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Quote Originally Posted by BEN10 View Post
    Let me explain it with a 16bit arithmetic. The value of INT_MIN for it will be -32768 and INT_MAX will be 32767. Thus when you negate INT_MIN you get 32768 but this can't be represented in it coz the maximum value of INT is 32767, thus it'll go to the other side and print the corresponding value which in this case is -32768(the same as INT_MIN). Would it had been 32769 it would have printed -32767 and so on.
    Oh! Thanks, this made it preety clear now.

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    For 2's complement, look here.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    In 2's-complement, INT_MIN == -INT_MAX-1. For instance, if INT_MIN is -128, then INT_MAX is 127. So the negative of INT_MIN is 128, which is out of range.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by brewbuck View Post
    In 2's-complement, INT_MIN == -INT_MAX-1. For instance, if INT_MIN is -128, then INT_MAX is 127. So the negative of INT_MIN is 128, which is out of range.
    And rolls over to -128

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Which of course is really just pure luck, because the behavior is undefined, since you are talking about signed values. It is not portable behavior.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by quzah View Post
    Which of course is really just pure luck, because the behavior is undefined, since you are talking about signed values. It is not portable behavior.


    Quzah.
    Yes, but that's no reason not to discuss what is actually happening, since we of course know that.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sure, I'm just being pedantic.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overflow and range checking for mul/div
    By Elysia in forum C++ Programming
    Replies: 28
    Last Post: 06-06-2008, 02:09 PM
  2. Stack overflow errors in 3 areas
    By ulillillia in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 03:20 PM
  3. Signed Char Overflow
    By coder8137 in forum C Programming
    Replies: 5
    Last Post: 11-17-2006, 08:25 AM
  4. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 06:16 AM
  5. Buffer overflow errors
    By EvBladeRunnervE in forum C Programming
    Replies: 2
    Last Post: 03-17-2004, 04:58 PM