Cryptography Reference
In-Depth Information
tmp
h1
h2
Add or Subtract
Figure 3-7: Arithmetic routines with negative numbers
6. Subtracting is similar, in Listing 3-33.
Listing 3-33: “huge.c” subtract with negative number support
void subtract( huge *h1, huge *h2 )
{
int result_sign;
// First compute sign of result, then compute magnitude
if ( compare( h1, h2 ) > 0 )
{
result_sign = h1->sign;
if ( h1->sign == h2->sign )
{
subtract_magnitude( h1, h2 );
}
else
{
add_magnitude( h1, h2 );
}
}
else
{
huge tmp;
// put h1 into tmp and h2 into h1 to swap the operands
set_huge( &tmp, 0 ); // initialize
copy_huge( &tmp, h1 );
copy_huge( h1, h2 );
if ( h1->sign == tmp.sign )
{
result_sign = !( h1->sign );
subtract_magnitude( h1, &tmp );
}
else
{
result_sign = !( h2->sign );
add_magnitude( h1, &tmp );
(Continued)
Search WWH ::




Custom Search