开发者

GMP IMPORT/EXPORT

开发者 https://www.devze.com 2023-03-11 22:17 出处:网络
I have a problem with import/export GMP function. I try to convert a negative and positive Big Integer but when i work with negative number

I have a problem with import/export GMP function. I try to convert a negative and positive Big Integer but when i work with negative number i lose the sign. Are there example about it?

Export:

                    int mydim = (mpz_sizeinbase(c, 2) +7)/ 8;
                    myb =  ( char*) malloc(sizeof(char) * mydim);
               开发者_如何学C     count = (size_t*) malloc(sizeof(size_t));

                    if(mpz_sgn(c)>=0){
                    mpz_export((void *) myb, count, 1, sizeof( char), 1, 0, c);
                            }
                            else{
                    mpz_add_ui(c,c,1);
                    mpz_export((void*)myb, count, 1, sizeof( char), 1, 0, c);
                    for(int i =0;i<=mydim;i++){ //This could be done more effectively
                            myb[i]=~myb[i];
                    }
                    }

Import:

            mpz_import(s, *count, 1, sizeof(myb[0]), 1, 0, myb);
            int sign = myb[0] < 0?-1:1;
            if(sign == -1)
                    mpz_neg(s,s);


The mpz_export documentation says:

The sign of op is ignored, just the absolute value is used.


compute complement of myb,and don't forget the sign bit for negative number.

0

精彩评论

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

关注公众号