gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:01:06 26 4
gpt4 key购买 nike

我想将另一个 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 (33bit)=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]='\0';
mpz_set_str(str,buf,2); // convert the first 23 bit to mpz variable

输出为:1101110110111100001010

关于c - 使用 gnump ( c ) 存储最高有效位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41652979/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com