[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bignums



-----BEGIN PGP SIGNED MESSAGE-----


  It isn't a question of just byte swapping.
  It is a question of whether a *multibyte* integer is stored as:
	(using decimal digits as bytes)

	0123456789

  vs
	9876543210

  It it were just byte swapping, we'd have to argue about:

	0123456789
  vs    1032547698
  vs    8967452301
  vs    9876543210
	 
  (yes, some should remind people of PDP-11)

  If the byte string is stored little endian, then when processing you
do:
	int   c;
	int   bits;
	char *bignum;

	while(bits > 8 && (c=getc()) != EOF)
	{
		*bignum++=c;
	}

  The bignum is now naturally alligned to whatever the bignum started
out being aligned to. (64 bits if you need it that way)
  If you store things big-endian, then you need to make sure that the
bignum is either 0-extended to a multiple of ??, or need to start
filling the array at the right point. 
  I agree with you that this is a trivial issue:

	int   c;
	int   bits;
	char *bignum;

	/* zero out bignum! */
	bignum = (bignum + bits/8 + sizeof(my_alignment_type) +	1)/sizeof(my_alignment_type);

	while(bits > 8 && (c=getc()) != EOF)
	{
		*bignum--=c;
	}

	
  For the little endian case, little endian machines win a microscopic
amount. For the big endian case, both machines are equal.
  (Of course, big endian machines should omit the "bits/8" from the
addition, and do exchange the ++/--.)

   :!mcr!:            |  Network security consulting and 
   Michael Richardson |      contract programming
 WWW: <A HREF="http://www.sandelman.ottawa.on.ca/People/Michael_Richardson/Bio.html">mcr@sandelman.ottawa.on.ca</A>. PGP key available.

-----BEGIN PGP SIGNATURE-----
Version: 2.6.3ia
Charset: latin1
Comment: Processed by Mailcrypt 3.4, an Emacs/PGP interface

iQB1AwUBM0WxwaZpLyXYhL+BAQGR/wL/VHRy6nwTCyKGqgMH+CkGO8GjtK/AfCbO
EL85bo8GoXAFxFoiRbWKsRcTNDULMZbNIODAhYB1kEPMGDqXgY7MukTsoo6yoTL/
trANUIyO6cHNtogE82ZfAdVbyskNY3s5
=acwU
-----END PGP SIGNATURE-----

References: