Detect Carry Flag?

I

Immortal Nephi

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...
 
V

Victor Bazarov

Immortal said:
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
 
J

James Kanze

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;

// 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.
 
K

Kirk Johnson

Immortal Nephi said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top