用gnump存储最重要的位(c)

时间:2017-01-14 17:38:38

标签: c biginteger

我想在mpz_t变量中存储另一个mpz_t变量的最高位。其实我想要左移位置。

根据手册,我使用了以下功能:

void mpz_mul_2exp (mpz_t rop, mpz_t op1, unsigned long int op2) /*
Set rop to op1 × 2op2. This operation can also be defined as a left shift by op2 bits*/

但是,我的二进制数为x(33位)= 11011101101111000010101100001010

当我使用上述功能时

 mpz_mul_2exp(shift,x,10);

输出为:1100001010。

我只想存储前23位(1101110110111100001010)。

1 个答案:

答案 0 :(得分:1)

我找到了。

mpz_t str;
//x: the number 11011101101111000010101100001010
char bbb[256],buf[mpz_sizeinbase(x,2)-10];    
mpz_get_str(bbb,2,x); // store the binary value
// cut the last 10 bits
for(int as=0;as<mpz_sizeinbase(x,2)-10;as++){
      buf[as]=bbb[as];
}
buf[strlen(bbb)-10]='\0';
mpz_set_str(str,buf,2); // convert the first 23 bit to mpz variable

输出为:1101110110111100001010