Detect Carry Flag?

Discussion in 'C++' started by Immortal Nephi, May 7, 2009.

  1. I do math calculation. I decide to limit the integer to 8 bits. If
    it exceeds 0xFF, then Carry flag should be set. C++ Compiler does not
    have the feature. I believe that it can be done to use if keyword.

    For example

    unsigned char Low_Byte = 0xFF;
    unsigned char High_Byte = 0x20;
    unsigned char Carry = 0x00;

    Low_Byte++;

    // Use IF to detect Carry
    // How?
    if (Carry ??? Low_Byte)
    {
    High_Byte++;
    }


    Please do not tell me to use short or long instead of char like this
    below.

    unsigned short Low_Byte = 0xFF;
    unsigned short High_Byte = 0x20;
    unsigned short Carry = 0;

    Low_Byte++;
    Carry = Low_Byte >> 8;
    Low_Byte &= 0xFF;
    High_Byte += Carry;

    Thanks...
     
    Immortal Nephi, May 7, 2009
    #1
    1. Advertising

  2. Immortal Nephi wrote:
    > I do math calculation. I decide to limit the integer to 8 bits. If
    > it exceeds 0xFF, then Carry flag should be set. C++ Compiler does not
    > have the feature. I believe that it can be done to use if keyword.
    >
    > For example
    >
    > unsigned char Low_Byte = 0xFF;
    > unsigned char High_Byte = 0x20;
    > unsigned char Carry = 0x00;
    >
    > Low_Byte++;
    >
    > // Use IF to detect Carry
    > // How?
    > if (Carry ??? Low_Byte)
    > {
    > High_Byte++;
    > }
    > [..]


    Well, as you've discovered already, the _language_ does not have that
    feature. Your compiler, however, might, so check the compiler
    documentation first. Second, you can always use inline assembler to
    perform increments and check CPU flags (again, compiler-specific). In
    most cases if you need to stay portable, it's better to *predict* carry
    than to *detect*:

    bool Carry = Low_Byte == UCHAR_MAX;
    Low_Byte++;
    // here 'Carry' is set.

    If you don't have to remember to check those operations, wrap your
    arithmetic in some kind of class, which will perform checks and set the
    flags, like

    class Arithmetic {
    public:
    bool Carry;
    static unsigned char post_inc(unsigned char& v) {
    Carry = v == UCHAR_MAX;
    return v++;
    }
    ...
    };

    Then you do

    Arithmetic::post_inc(Low_Byte);
    if (Arithmetic::Carry) ...

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, May 7, 2009
    #2
    1. Advertising

  3. Immortal Nephi

    James Kanze Guest

    On May 7, 8:00 pm, Immortal Nephi <> wrote:
    > I do math calculation. I decide to limit the integer to 8
    > bits. If it exceeds 0xFF, then Carry flag should be set. C++
    > Compiler does not have the feature. I believe that it can be
    > done to use if keyword.


    > For example


    > unsigned char Low_Byte = 0xFF;
    > unsigned char High_Byte = 0x20;
    > unsigned char Carry = 0x00;


    > Low_Byte++;


    > // Use IF to detect Carry
    > // How?
    > if (Carry ??? Low_Byte)


    if ( Low_Byte == 0 ) {

    > {
    > High_Byte++;
    > }


    > Please do not tell me to use short or long instead of char
    > like this below.


    > unsigned short Low_Byte = 0xFF;
    > unsigned short High_Byte = 0x20;
    > unsigned short Carry = 0;


    > Low_Byte++;
    > Carry = Low_Byte >> 8;


    This is guaranteed to set Carry to 0 (supposing 8 bit bytes,
    which is the case on most machines).

    > Low_Byte &= 0xFF;
    > High_Byte += Carry;


    You can always do:
    Carry = Low_Byte + 1 > UCHAR_MAX ;
    ++ Low_Byte ;
    That will work on most machines (where sizeof(int) > 1). More
    generally, you can check for overflow before doing the addition,
    and then act in consequence. If there is a larger integral type
    available, however, the simplest solution is just to use it for
    the intermediate values. Unless all you're doing is
    incrementing and decrementing unsigned integers---in the case of
    incrementing, if the results are 0, there was a carry, and if
    the case of decrementing, if the results were the maximum for
    the type.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, May 8, 2009
    #3
  4. Immortal Nephi

    James Kanze Guest

    On May 8, 2:01 pm, Pete Becker <> wrote:
    > James Kanze wrote:
    > > On May 7, 8:00 pm, Immortal Nephi <> wrote:
    > >> Please do not tell me to use short or long instead of char
    > >> like this below.


    > >> unsigned short Low_Byte = 0xFF;
    > >> unsigned short High_Byte = 0x20;
    > >> unsigned short Carry = 0;


    > >> Low_Byte++;
    > >> Carry = Low_Byte >> 8;


    > > This is guaranteed to set Carry to 0 (supposing 8 bit bytes,
    > > which is the case on most machines).


    > Um, note that the type of Low_Byte has changed: it's now
    > unsigned short. This code handles the carry correctly.


    Oops. I'd missed that.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, May 8, 2009
    #4
  5. Immortal Nephi

    Kirk Johnson Guest

    "Immortal Nephi" <> wrote in message
    news:...
    >I do math calculation. I decide to limit the integer to 8 bits. If
    > it exceeds 0xFF, then Carry flag should be set. C++ Compiler does not
    > have the feature. I believe that it can be done to use if keyword.
    >
    > For example
    >
    > unsigned char Low_Byte = 0xFF;
    > unsigned char High_Byte = 0x20;
    > unsigned char Carry = 0x00;
    >
    > Low_Byte++;
    >
    > // Use IF to detect Carry
    > // How?
    > if (Carry ??? Low_Byte)
    > {
    > High_Byte++;
    > }
    >
    >
    > Please do not tell me to use short or long instead of char like this
    > below.
    >
    > unsigned short Low_Byte = 0xFF;
    > unsigned short High_Byte = 0x20;
    > unsigned short Carry = 0;
    >
    > Low_Byte++;
    > Carry = Low_Byte >> 8;
    > Low_Byte &= 0xFF;
    > High_Byte += Carry;
    >
    > Thanks...






    Stretch my old ring out.

    I Am Kirk Johnson.
    "Anal Stretching, Wrenching & Expanding Specialist"
    http://www.imagefap.com/image.php?id=1988478267
     
    Kirk Johnson, May 10, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jan Kindt
    Replies:
    1
    Views:
    1,532
    Jim Lewis
    Sep 17, 2003
  2. Tuukka Toivonen

    Using carry-in adders with Synopsys

    Tuukka Toivonen, Dec 1, 2003, in forum: VHDL
    Replies:
    3
    Views:
    1,549
    Ian Poole
    Dec 2, 2003
  3. Rune Christensen
    Replies:
    4
    Views:
    10,067
    David R Brooks
    Jan 22, 2005
  4. Damien Bardon

    Get the carry with add operator

    Damien Bardon, Mar 20, 2006, in forum: VHDL
    Replies:
    5
    Views:
    4,960
    Mike Treseler
    Mar 21, 2006
  5. rickman
    Replies:
    9
    Views:
    795
Loading...

Share This Page