Cryptography Reference
In-Depth Information
Table 3-1 (continued)
DECIMAL
BINARY
9
1001
10
1010
11
1011
12
1100
13
1101
14
1110
¨
a. start here, go backward 15 steps
15
1111
¨
c. end here
Because 15 is greater than 14, you set the borrow bit and subtract one extra
from the preceding char , ending up with the representation (0 15) (0000 1111),
which is the correct answer.
To be a bit more memory effi cient, you should also contract the response
before returning. That is, look for extraneous chars ints on the left side and
remove them, as shown in Listing 3-8.
Listing 3-8: “huge.c” contract
/**
* Go through h and see how many of the left-most bytes are unused.
* Remove them and resize h appropriately.
*/
void contract( huge *h )
{
int i = 0;
while ( !( h->rep[ i ] ) && ( i < h->size ) ) { i++; }
if ( i && i < h->size )
{
unsigned char *tmp = &h->rep[ i ];
h->rep = ( unsigned char * ) calloc( h->size - i,
sizeof( unsigned char ) );
memcpy( h->rep, tmp, h->size - i );
h->size -= i;
}
}
This happens in two steps. First, fi nd the leftmost non-zero char , whose posi-
tion is contained in i . Second, resize the array representation of h . This works,
obviously, just like expansion but in reverse.
As shown earlier, addition and subtraction are somewhat straightforward;
you can take advantage of the underlying machine architecture to a large extent;
Search WWH ::




Custom Search