Pick up an hex digit from a mpz_t integer

Ramón T. B. framontb at yahoo.es
Sun Jul 28 13:59:36 CEST 2013



Thanks !!!

The two variants works well:

char pick_hex_z(mpz_t z_number, int position) {
char * str_number = mpz_get_str(NULL, 16, z_number);
int len = strlen(str_number);
if (position < len) {
return str_number[len-position];
} else return '0';  // return 0 if position > number of hex digits in z_number
}


unsigned int pick_hex(mpz_t z_number, size_t position) {
size_t sz = mpz_sizeinbase (z_number, 16);
position--;  // because I want to start counting at one
if (position <= sz){
mpz_t tmp_z, mask;
mpz_inits(tmp_z, mask, NULL);
mpz_init_set_ui(mask, 15); // binary 1111
mpz_fdiv_q_2exp(tmp_z, z_number, position<<2); // right shift z by 4n places
mpz_and(tmp_z, tmp_z, mask); // chop off least significant nybble
unsigned int result = mpz_get_ui(tmp_z);
mpz_clears(tmp_z, mask, NULL);
return result;
} else return 0;  // return 0 if position > number of hex digits in z_number
}

Regards

________________________________
 De: David Gillies <daggillies at gmail.com>
Para: "gmp-discuss at gmplib.org" <gmp-discuss at gmplib.org> 
CC: Ramón T. B. <framontb at yahoo.es> 
Enviado: Domingo 28 de julio de 2013 0:13
Asunto: Re: Pick up an hex digit from a mpz_t integer
 


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