开发者

How to find the greatest power of 2 dividing a number using GMP?

开发者 https://www.devze.com 2023-01-21 14:23 出处:网络
According to the GMP documentation here: Function: unsigned long int mpz_remove (mpz_t rop, mpz_t op, mpz_t f)

According to the GMP documentation here:

Function: unsigned long int mpz_remove (mpz_t rop, mpz_t op, mpz_t f)

Remove all occurrences of the factor f from op and store the result in rop. The return value is how many such occurrences were removed.

So the mpz_remove function should be able to be used to answer the titled question. At the moment my code looks like this:

  mpz_set_ui(temp2,2);
  mpz_remove(temp,K0,temp2);

which works fine, but the result I want is K0 divided by temp (and not temp itself) [which I could get by adding a subsequent division operation, but that seems wasteful].

How should I ac开发者_如何转开发tually get K0/temp?


You might try the combination of mpz_scan1() and mpz_tdiv_q_2exp().

mpz_tdiv_q_2exp(result,K0,mpz_scan1(K0,0))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号