使用 gnump ( c ) 存储最高有效位

store most significant bit with gnump ( c )

我想在一个 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)。

找到了

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]='[=10=]';
mpz_set_str(str,buf,2); // convert the first 23 bit to mpz variable

输出为:1101110110111100001010