Pick up an hex digit from a mpz_t integer
Pedro Gimeno
gmpdiscuss at formauri.es
Mon Jul 29 16:58:04 CEST 2013
David Gillies wrote, On 2013-07-28 00:13:
> If you're pulling digits out of any base 2^n representation then mask and
> shift are likely to be the fastest approach.
>
> get nth hex digit of (positive) integer z (haven't tested this - it's off
> the top of my head):
>
> mpz_t tmp_z,mask;
> mpz_init_set_ui(mask,15); // binary 1111
> mpz_fdiv_q_2exp(tmp_z,z,n<<2); // right shift z by 4n places
> mpz_and(tmp_z,mask); // chop off least significant nybble
>
> tmp_z is the nth hex digit.
Best performance (without taking advantage of GMP internal structures,
only of internal format) can probably be obtained through mpz_getlimbn,
but the function is going to be a bit complicated comparatively if it's
intended to be portable or if the target libgmp uses nails.
Other than that, it's probably preferable to mask using C masking rather
than mpz masking to save one of the temporaries.
-- Pedro Gimeno
More information about the gmp-discuss
mailing list