overflow...?

This is a discussion on overflow...? within the C Programming forums, part of the General Programming Boards category; Code: int a= INT_MIN; a = -a; printf("%d", a); a still has a negative value! Why? It also mentions an ...

  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
    21,718
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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,189
    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
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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, 07:25 AM
  4. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 05:16 AM
  5. Buffer overflow errors
    By EvBladeRunnervE in forum C Programming
    Replies: 2
    Last Post: 03-17-2004, 03:58 PM

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