[英]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(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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.