Cryptography Reference
In-Depth Information
if ( borrow && i )
{
if ( !( h1->rep[ i - 1 ] ) ) // Don't borrow i
{
printf( “Error, subtraction result is negative\n” );
exit( 0 );
}
h1->rep[ i - 1 ]--;
}
contract( h1 );
}
The subtract routine looks a lot like the add routine, but in reverse. Note that
there's no allocation of space at the beginning. Because you're subtracting, the
result always uses up less space than what you started with. Also, there's no
provision in this library yet for negative numbers, so behavior in this case is
undefi ned if h2 is greater than h1 .
Otherwise, subtracting is pretty much like adding: You work backward, keep-
ing track of the borrow from the previous char at each step. Again, although the
subtraction operation wraps if h2->rep[ j ] > h1->rep[ i ] , the wrap ends
up in the right position. To see this, consider the subtraction 30 - 15 (binary
11110 - 1111). To keep thi ngs simple, imagi ne that a char is four bits. The integer
30 then takes up two four-bit char s and is represented as:
(0001 1110) : (1 14 )
whereas 15 takes up one char and is represented as 1111 (15) . When sub-
tracting, start by subtracting 15 from 14 and end up “wrapping” back to 15 as
illustrated in Table 3-1.
Table 3-1: Subtraction Wrapping Behavior
DECIMAL
BINARY
0
0000
¨
b. wrap back around to the bottom
1
0001
2
0010
3
0011
4
0100
5
0101
6
0110
7
0111
8
1000
(Continued)
 
Search WWH ::




Custom Search