Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Detect Carry Flag?

Reply
Thread Tools

Detect Carry Flag?

 
 
Immortal Nephi
Guest
Posts: n/a
 
      05-07-2009
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...
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-07-2009
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:ost_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
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      05-08-2009
On May 7, 8:00 pm, Immortal Nephi <(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-08-2009
On May 8, 2:01 pm, Pete Becker <(E-Mail Removed)> wrote:
> James Kanze wrote:
> > On May 7, 8:00 pm, Immortal Nephi <(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
Kirk Johnson
Guest
Posts: n/a
 
      05-10-2009

"Immortal Nephi" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using carry chain of counters for term count detect rickman VHDL 9 08-21-2009 02:23 PM
Get the carry with add operator Damien Bardon VHDL 5 03-21-2006 01:27 PM
how do you extract carry, borrow and overflow from an adder in vhdl? Rune Christensen VHDL 4 01-22-2005 11:17 PM
Using carry-in adders with Synopsys Tuukka Toivonen VHDL 3 12-02-2003 12:23 PM
Counter with carry out at embedded bit. Jan Kindt VHDL 1 09-17-2003 07:28 PM



Advertisments