New function mpz_init_setbit

marco.bodrato at tutanota.com marco.bodrato at tutanota.com
Thu Jun 25 17:48:59 CEST 2026


Ciao,

25 giu 2026, 16:41 da hermann at stamm-wilbrandt.de:

> Is that function important enough, or does it bloat the API?
>

It is not "important" it can be useful, and it's easy to maintain.

It is an alternative to the function mpz_init2, which also initialize the variable to a
possibly useful value of the given size.

Thanks to the "lazy allocation" we implemented a while ago,
it is not even much important for speed,
it only helps in initializing with a given not tiny value.
> mpz_t one, result;
> mpz_init(one);
> mpz_set_ui(one, 1);
>
I'd suggest
 mpz_init_set_ui (one, 1);

>
> what mpz_set_2exp() does can be done with existing
>
> mpz_mul_2exp(result, one, 17);
>
Maybe you forgot to initialize "result".
If it is already initialized, I'd suggest not to depend on "one":
 mpz_set_ui (result, 0);
 mpz_setbit (result, 17);

If it is not:
 mpz_init2 (result, 18);
 mpz_setbit (result, 17);

Ĝis,
m


More information about the gmp-devel mailing list