Richard Cavell wrote:[color=blue]
> I wish to write an algorithm in C++. My intention is to run it on a Mac
> G4, however it would be nice to have the same program compile and run on
> a Pentium 4.[/color]
[snip][color=blue]
> Also, is there a way of testing for carry? For example,
>
> int a = something;
> int b = something;
> int c = a+b;
>
> if (carry bit is set) do something;[/color]
"Carry" is universally understood only for unsigned addition.
Subtraction or signed quantities may cause confusion.
Some machines (such as x86) set the Carry bit after the subtraction
(x - y) to be the Borrow:
CarryOut(3 - 4) is 1.
Other machines (such as PowerPC) set the Carry bit after the subtraction
(x - y) to be the Carry from (x + ~y + 1) [both additions done
by the same adder at the same time, by setting the CarryIn to 1]
which is the complement of the Borrow:
CarryOut(3 - 4) is 0.
Depending on implementation technology, the PowerPC convention may
save a few hardware logic gates. The x86 convention is better for
many programming tasks because it saves instructions when the Borrow
of a subtraction is used as the CarryIn of a following addition or
logical operation.
--
John Reiser,
jreiser@BitWagon.com