views:

70

answers:

1

Using GMP on c, I have a big integer "mpz_t n" in decimal form, how can I cut it into 2 parts? In fact, these 2 parts should have the same length in binary.

For example, maybe I can convert the n to a binary of 112bits, then I want to cut it into 2 56bits parts.

Thanks

A: 

I would use temp = mpz_sizeinbase(n,2) to get the number of bits in your original number and then use mpz_tdiv_q_2exp(q, n, temp>>1) and mpz_tdiv_r_2exp(r, n, temp>>1) to get the upper and lower halves of your original number.

Depending on how you want to handle an odd bit length, you may need to adjust the calculation of temp>>1.

HTH, casevh

casevh
thanx, it seems much better than the method i use now. thanx a lot