Cryptography Reference
In-Depth Information
right_shift( divisor );
}
bit_position++;
}
while ( bit_size-- );
}
Start by left shifting the divisor until it's greater than or equal to the dividend.
Most of the time, this means you “overshoot” a bit, but that's not a problem
because you compare again when you start the actual division.
while ( compare( divisor, dividend ) < 0 )
{
left_shift( divisor );
bit_size++;
}
Comparing Large Numbers
Notice the call to the compare function. Remember the subtract function a while
ago — in theory, you could just call subtract here, and check to see if the result
is negative. Two problems with that approach are that a) subtract overwrites
its fi rst operator, and b) you don't have any provision for negative numbers.
Of course, you could work around both of these, but a new compare function,
shown in Listing 3-14, serves better.
Listing 3-14: “huge.c” compare
/**
* Compare h1 to h2. Return:
* 0 if h1 == h2
* a positive number if h1 > h2
* a negative number if h1 < h2
*/
int compare( huge *h1, huge *h2 )
{
int i, j;
if ( h1->size > h2->size )
{
return 1;
}
if ( h1->size < h2->size )
{
return -1;
}
// Otherwise, sizes are equal, have to actually compare.
(Continued)
 
Search WWH ::




Custom Search