GMP 进口/出口
我的导入/导出
GMP 功能有问题。 我尝试转换负数和正大整数,但是当我使用负数时,我会丢失符号。 有相关的例子吗?
出口:
int mydim = (mpz_sizeinbase(c, 2) +7)/ 8;
myb = (char*) malloc(sizeof(char) * mydim);
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];
}
}
进口:
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);
I have a problem with the 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 any examples about it?
Export:
int mydim = (mpz_sizeinbase(c, 2) +7)/ 8;
myb = (char*) malloc(sizeof(char) * mydim);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
mpz_export 文档 说:
The mpz_export documentation says:
计算 myb 的补码,不要忘记负数的符号位。
compute complement of myb,and don't forget the sign bit for negative number.