I think the problem is that you are mixing C++ and C. The mpz_t type is the C data type, but you are trying to print it using the C++ cout function.
You want to use gmp_printf to print the C data types. Another problem is you have not called mpz_init on any of the mpz_t's that you created. Bill. On 22 December 2014 at 15:41, newcoder <hf.ba...@gmail.com> wrote: > Hi, > > Thanks a lot for accepting my request to be a member of this group. > > This is my first attempt at using MPIR, so please excuse me if you find > something silly :-) > > I was looking for a way to convert very large decimals numbers into binary > using C. Since I did not find anything I thought of writing my own and > started exploring MPIR. There might be some easier alternatives but I am > using MPIR because I have to do some more maths on big numbers. > > What I am trying to do is input a decimal number and then convert this > into binary. My program works fine till it find the number of bits > required. However something is wrong in the way I am using the operations > in the second last `for` loops, i.e: > > kb[i] = b % 2; > b /= 2; > > > > This is what I am trying to convert into MPIR specific operations using > mpz_mod() and mpz_cdiv_q(). What I have done is: > > > > mpz_t d; > mpz_init (d); > mpz_set_str (d, "2" , 10); //because I want to find (something)mod2 , d > contains 2 now > > for (int i = nob; i > 0; i--) > { > mpz_mod(kb[i], b, d); // kb[i] = b % 2 > mpz_cdiv_q(b, b, d ); // b /= 2; > > } > > for (int i = nob; i > 0; i--) > { > cout<<kb[i]<<"\n"; > } > > This prints: > > Please, enter the decimal value: 100 > 7 > 0 > 0 > 1 > 1 > 1 > 0 > 0 > Press any key to continue . . . > > Which is wrong. The correct is 01100100 > > > Here is my code: > > int main() > > { > > mpz_t b ; > size_t nob; //no. of bits will be here > > mpz_init (b); //Initialize > > char k[80]; //My decimal as an string will go here > > cout << "Please, enter the decimal value: "; > > cin.getline(k, sizeof k); //decimal value stored as string here > > mpz_set_str (b, k , 10); //transfer the string into b, assuming base > 10 > > nob = mpz_sizeinbase(b, 2); // find total number of bits in b > > cout<<nob<<"\n"; > > > mpz_t d; > mpz_init (d); > mpz_set_str (d, "2" , 10); //because I want to find (something)mod2 > > mpz_t kb[80000]; //Array. will store my converted binary bits in kb > for (int i = nob; i > 0; i--) > { > mpz_mod(kb[i], b, d); // implementing kb[i] = b % 2 > mpz_cdiv_q(b, b, d ); // b /= 2; > > } > > for (int i = nob; i > 0; i--) > { > cout<<kb[i]<<"\n"; > } > > return 0; > > } > > > > Can someone help me solve this issue? > > Thanks in advance for your valuable inputs. > > > > Note: I did not know that the request will be accepted so quickly, so > posted it on stackoverflow also , which I can't delete now. > > -- > You received this message because you are subscribed to the Google Groups > "mpir-devel" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to mpir-devel+unsubscr...@googlegroups.com. > To post to this group, send email to mpir-devel@googlegroups.com. > Visit this group at http://groups.google.com/group/mpir-devel. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "mpir-devel" group. To unsubscribe from this group and stop receiving emails from it, send an email to mpir-devel+unsubscr...@googlegroups.com. To post to this group, send email to mpir-devel@googlegroups.com. Visit this group at http://groups.google.com/group/mpir-devel. For more options, visit https://groups.google.com/d/optout.