Pick up an hex digit from a mpz_t integer

David Gillies daggillies at gmail.com
Sun Jul 28 00:13:39 CEST 2013


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.




On Sat, Jul 27, 2013 at 11:45 AM, Sam Rawlins <sam.rawlins at gmail.com> wrote:

> Your example does not use any mpz_t integers, but I'll give you the benefit
> of the doubt, and assume you mean that number is an mpz_t, and not a char*:
>
> If you have an mpz_t, number, you can convert to a hexadecimal
> representation in a char* with mpz_get_str() [1], like so:
>
>     mpz_get_str(my_char_pointer, 16, number);
>
> From there, you can use standard C and string functions to pluck out the
> hexadecimal digit you are looking for.
>
> [1] http://gmplib.org/manual/Converting-Integers.html#Converting-Integers
>
>
> On Sat, Jul 27, 2013 at 10:19 AM, Ramón T. B. <framontb at yahoo.es> wrote:
>
> > Hello all,
> >
> >    I need a function to pick up an hex digit from a mpz_t integer. For
> > example:
> >
> > int pick_hex(mpz_t number, int position);
> >
> > For example:
> > number = "123456789abcdef";
> > position = 3;
> > solution = pick_hex(number, position);  // solution = c
> >
> > How I can achieve that?
> >
> > Thanks in advance !
> > _______________________________________________
> > gmp-discuss mailing list
> > gmp-discuss at gmplib.org
> > https://gmplib.org/mailman/listinfo/gmp-discuss
> >
>
>
>
> --
> Sam Rawlins
> _______________________________________________
> gmp-discuss mailing list
> gmp-discuss at gmplib.org
> https://gmplib.org/mailman/listinfo/gmp-discuss
>



-- 
David Gillies
San Jose
Costa Rica


More information about the gmp-discuss mailing list