简体   繁体   中英

store most significant bit with gnump ( c )

I want to store in an mpz_t variable the most significant bits of another mpz_t variable. Actually I want the left shift locigal.

According to the manual, I use the followin function:

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*/

But, I have the number x in binary (33bit)= 11011101101111000010101100001010

And when I use the above function

 mpz_mul_2exp(shift,x,10);

The output is: 1100001010.

I want to store only the first 23 bits (1101110110111100001010).

I found it.

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

The output is : 1101110110111100001010

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM