This is my modified code. I forgot initialization at some of the lines.
Also now I am using gmp_printf
int main()
{
mpz_t d ; //Will hold my big decimal value
size_t nob; // no. Of bits
mpz_init (d); // My decximal value is being initialised here
char decimalValue[800];
cout << "Please, enter the decimal value: ";
cin.getline(decimalValue, sizeof decimalValue);
mpz_set_str (d, decimalValue , 10); //Transfering decimalValue
into d , decimalValue is base 10
nob = mpz_sizeinbase(d, 2); //Finds how many bits are there in my binary
cout<<nob<<"\n"; //Verfied my No. of bits
mpz_t base; //Will be used for modulo
mpz_init (base); //initialization
mpz_set_str (base, "2" , 10); //since I want modulo 2
mpz_t kb[3000]; //will hold my binary bits
mpz_array_init (kb[0], 3000, 1); //initialize my array
for (int i = nob; i > 0; i--)
{
mpz_mod(kb[i], d, base); // Finds d % base, and puts in kb[i]
mpz_cdiv_q(d, d, base ); // Finds d / base, and puts in d
}
for (int i = nob; i > 0; i--)
{
gmp_printf ("%s in decimalValue %Zd %c\n", "These are the bits ", kb[i],
decimalValue[i]);
}
And here is the same output:
Please, enter the decimal value: 100
> 7
> These are the bits in decimalValue 0
> These are the bits in decimalValue 0
> These are the bits in decimalValue 1
> These are the bits in decimalValue 1
> These are the bits in decimalValue 1
> These are the bits in decimalValue 0 0
> These are the bits in decimalValue 0 0
> Press any key to continue . . .
What is wrong here ?
On Monday, December 22, 2014 9:21:37 PM UTC+5:30, Bill Hart wrote:
>
> 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 <[email protected] <javascript:>>
> 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 [email protected] <javascript:>.
>> To post to this group, send email to [email protected]
>> <javascript:>.
>> Visit this group at http://groups.google.com/group/mpir-devel.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
On Monday, December 22, 2014 9:21:37 PM UTC+5:30, Bill Hart wrote:
>
> 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 <[email protected] <javascript:>>
> 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 [email protected] <javascript:>.
>> To post to this group, send email to [email protected]
>> <javascript:>.
>> 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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/mpir-devel.
For more options, visit https://groups.google.com/d/optout.