Thread: signed number addition

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    15

    signed number addition

    Hi,

    i'm writing a program that adds 2 4bit numbers and check if there is an overflow.


    let's say i have A and B and each of them is 4 bits

    i know if they have different signs, i wouldn't have overflow

    my question is that how can i check for overflow after adding the A and B

    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If your processor supports larger number of bits than 4 - which it's bound to do if you have a C-compiler working on it. So calculate using 8, 16 or 32-bit signed numbers (sign-extending the number to make it the longer size). Now check if the next bit up is the same sign as the sign-bit. If it isn't, you got an overflow.

    --
    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
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    What about this:
    Code:
    if ( ((nibble >> 4) << 4) == 0 )
    {
        ...
    }

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Lind View Post
    my question is that how can i check for overflow after adding the A and B
    How are you doing the addition? Are you doing it yourself, bit-by-bit, or are you just using the '+' operator?

    If you are adding by bit, all you need to do is see if the carry bit is set at the end. If you are using '+' to simulate 4-bit addition inside a larger integer type, just look to see if the value is greater than 7. If it is, there was overflow.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cpjust View Post
    What about this:
    Code:
    if ( ((nibble >> 4) << 4) == 0 )
    {
        ...
    }
    Which is also equivalent to:

    Code:
    if((nibble & ~0x07) == 0)
    Which is also equivalent to:

    Code:
    if(nibble > 7)

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    58
    There will be an overflow only if both "most significant bits" are 1.

    (in your case, that's the 3rd bit from the right)

    http://en.wikipedia.org/wiki/Most_significant_bit


    EDIT: Sorry, that was wrong.
    Last edited by Govalant; 09-11-2007 at 09:29 PM.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Govalant View Post
    There will be an overflow only if both "most significant bits" are 1.
    No. If the leftmost bits are 0 and 1, or 1 and 0, you could still overflow if there is a carry. For instance, 1111 + 0111. The MSB's are not both 1, but it overflows.

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    58
    Right, i was thinking kind of the opposite. If both MSB are 0 there will be no overflow.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    INT_MAX - A < B
    if this is true, then A + B would be > INT_MAX, hence overflow.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    How do you come to have 4-bit numbers? Are you using bitfields?
    If so, you can simply check if the answer is less than one of the operands:
    Code:
    struct foo {
    	unsigned int bar:4;
    };
    
    foo x, y, z;
    x.bar = 12;
    y.bar = 8;
    z.bar = x.bar + y.bar;
    if (z.bar < x.bar)
    {
    	//Overflow occurred
    }
    Edit: Oh you said unsigned, which most of us seem to have missed...
    In that case, even what Salem posted wont work directly either.
    Um, this is for two's complement right? (don't want to make any other wrong assumptions)
    Last edited by iMalc; 09-12-2007 at 01:47 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    How about this:
    Code:
    struct foo {
    	int bar : 4;
    };
    
    foo x, y, z;
    x.bar = -6;
    y.bar = -5;
    z.bar = x.bar + y.bar;
    if ((int)z.bar != (int)x.bar + (int)y.bar)
    {
    	//Overflow occurred
    }
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Learning Memory, Ins and Outs?
    By Zoiked in forum C Programming
    Replies: 1
    Last Post: 08-27-2007, 04:43 PM
  2. Finding a number within a number
    By jeev2005 in forum C Programming
    Replies: 2
    Last Post: 01-10-2006, 08:57 PM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM