New function mpz_init_setbit

hermann at stamm-wilbrandt.de hermann at stamm-wilbrandt.de
Thu Jun 25 16:41:02 CEST 2026


On 2026-06-25 16:18, Torbjörn Granlund wrote:
> marco.bodrato at tutanota.com writes:
> 
>   Do you mind if I add a new function?
> 
>   Here is its implementation:
> 
>   /* mpz_init_setbit(integer, val) -- Initialize and assign INTEGER
> with 2^VAL. */
>   void
>   mpz_init_setbit (mpz_ptr dest, mp_bitcnt_t bit_idx)
>   {
>     mp_ptr dp;
>     mp_size_t limb_idx = bit_idx / GMP_NUMB_BITS;
>     mp_limb_t mask = CNST_LIMB(1) << (bit_idx % GMP_NUMB_BITS);
> 
>     ALLOC (dest) = SIZ (dest) = limb_idx + 1;
>     PTR (dest) = dp = __GMP_ALLOCATE_FUNC_LIMBS (limb_idx + 1);
>     MPN_ZERO (dp, limb_idx);
>     dp[limb_idx] = mask;
>   }
> 
> I suppose that the name should be mpz_set_2exp, for consistency?
> 
Is that function important enough, or does it bloat the API?

With

     mpz_t one, result;
     mpz_init(one);
     mpz_set_ui(one, 1);

what mpz_set_2exp() does can be done with existing

     mpz_mul_2exp(result, one, 17);


More information about the gmp-devel mailing list