C: Converting a very large integer into a binary
up vote
0
down vote
favorite
I am trying to convert decimal Nos. into binary. The code works pretty fine (Windows 7 , 32 bit MS-VS2010):
int main()
{
int k, n;
int binary[100];
printf("Enter the value in decimal n ");
scanf("%d", &k);
n = (log(k * 1.0) / log(2 * 1.0)) + 1 ; //total no. of binary bits in this decimal
for (int i = n; i > 0; i--)
{
binary[i] = k % 2;
k /= 2;
}
return 0;
}
But the limitation is that it works for Int size values only i.e. 32 bit. I want to modify this code so that it works for 2048 bits (decimal numbers containing 617 digits actually). I am not allowed to use any library.
Can someone give me some pointers how to proceed to tackle this?
Can someone give an example code snippet say for 64 bits ? Then I can use this to extend to higher values.
Update
1-As per suggestions I am trying to use strings. But I am not able to understand how to convert an String into large Int (I cant use stoi() as thsi will convert to 32 bit int , right? ).
2- Secondly I have to find:
log(222121212213212313133123413131313131311313154515441315413451315641314563154134156313461316413415635154613415645156451434)
Is the library function log
capable of finding this ? Then what is the solution?
c
|
show 6 more comments
up vote
0
down vote
favorite
I am trying to convert decimal Nos. into binary. The code works pretty fine (Windows 7 , 32 bit MS-VS2010):
int main()
{
int k, n;
int binary[100];
printf("Enter the value in decimal n ");
scanf("%d", &k);
n = (log(k * 1.0) / log(2 * 1.0)) + 1 ; //total no. of binary bits in this decimal
for (int i = n; i > 0; i--)
{
binary[i] = k % 2;
k /= 2;
}
return 0;
}
But the limitation is that it works for Int size values only i.e. 32 bit. I want to modify this code so that it works for 2048 bits (decimal numbers containing 617 digits actually). I am not allowed to use any library.
Can someone give me some pointers how to proceed to tackle this?
Can someone give an example code snippet say for 64 bits ? Then I can use this to extend to higher values.
Update
1-As per suggestions I am trying to use strings. But I am not able to understand how to convert an String into large Int (I cant use stoi() as thsi will convert to 32 bit int , right? ).
2- Secondly I have to find:
log(222121212213212313133123413131313131311313154515441315413451315641314563154134156313461316413415635154613415645156451434)
Is the library function log
capable of finding this ? Then what is the solution?
c
5
Read the number as a string instead of anint
. Then process the digits in the string.
– Housy
Dec 18 '14 at 13:24
2
You don't need a library, just take the input string and convert.
– 2501
Dec 18 '14 at 13:25
3
If you're learning, there's no point in using libraries as there's not much you'll learn. You'll just get the end result directly and things will seem like magic, you wouldn't know how it happened. However, for production code, instead of reinventing the wheel, maintaining the bugs that ensue, using a time-tested library, if that's an option, is the way to go. Even then, you should choose a library and use it with some knowledge on what it does.
– legends2k
Dec 18 '14 at 13:28
1
Here are some excellent pointers.
– bitmask
Dec 18 '14 at 14:24
1
You're already using libc and libm if you haveprintf
,scanf
, andlog
, so what are your actual restrictions, just the stdlib?
– Ryan Haining
Dec 18 '14 at 16:19
|
show 6 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to convert decimal Nos. into binary. The code works pretty fine (Windows 7 , 32 bit MS-VS2010):
int main()
{
int k, n;
int binary[100];
printf("Enter the value in decimal n ");
scanf("%d", &k);
n = (log(k * 1.0) / log(2 * 1.0)) + 1 ; //total no. of binary bits in this decimal
for (int i = n; i > 0; i--)
{
binary[i] = k % 2;
k /= 2;
}
return 0;
}
But the limitation is that it works for Int size values only i.e. 32 bit. I want to modify this code so that it works for 2048 bits (decimal numbers containing 617 digits actually). I am not allowed to use any library.
Can someone give me some pointers how to proceed to tackle this?
Can someone give an example code snippet say for 64 bits ? Then I can use this to extend to higher values.
Update
1-As per suggestions I am trying to use strings. But I am not able to understand how to convert an String into large Int (I cant use stoi() as thsi will convert to 32 bit int , right? ).
2- Secondly I have to find:
log(222121212213212313133123413131313131311313154515441315413451315641314563154134156313461316413415635154613415645156451434)
Is the library function log
capable of finding this ? Then what is the solution?
c
I am trying to convert decimal Nos. into binary. The code works pretty fine (Windows 7 , 32 bit MS-VS2010):
int main()
{
int k, n;
int binary[100];
printf("Enter the value in decimal n ");
scanf("%d", &k);
n = (log(k * 1.0) / log(2 * 1.0)) + 1 ; //total no. of binary bits in this decimal
for (int i = n; i > 0; i--)
{
binary[i] = k % 2;
k /= 2;
}
return 0;
}
But the limitation is that it works for Int size values only i.e. 32 bit. I want to modify this code so that it works for 2048 bits (decimal numbers containing 617 digits actually). I am not allowed to use any library.
Can someone give me some pointers how to proceed to tackle this?
Can someone give an example code snippet say for 64 bits ? Then I can use this to extend to higher values.
Update
1-As per suggestions I am trying to use strings. But I am not able to understand how to convert an String into large Int (I cant use stoi() as thsi will convert to 32 bit int , right? ).
2- Secondly I have to find:
log(222121212213212313133123413131313131311313154515441315413451315641314563154134156313461316413415635154613415645156451434)
Is the library function log
capable of finding this ? Then what is the solution?
c
c
edited Dec 18 '14 at 14:33
asked Dec 18 '14 at 13:22
user3891236
332314
332314
5
Read the number as a string instead of anint
. Then process the digits in the string.
– Housy
Dec 18 '14 at 13:24
2
You don't need a library, just take the input string and convert.
– 2501
Dec 18 '14 at 13:25
3
If you're learning, there's no point in using libraries as there's not much you'll learn. You'll just get the end result directly and things will seem like magic, you wouldn't know how it happened. However, for production code, instead of reinventing the wheel, maintaining the bugs that ensue, using a time-tested library, if that's an option, is the way to go. Even then, you should choose a library and use it with some knowledge on what it does.
– legends2k
Dec 18 '14 at 13:28
1
Here are some excellent pointers.
– bitmask
Dec 18 '14 at 14:24
1
You're already using libc and libm if you haveprintf
,scanf
, andlog
, so what are your actual restrictions, just the stdlib?
– Ryan Haining
Dec 18 '14 at 16:19
|
show 6 more comments
5
Read the number as a string instead of anint
. Then process the digits in the string.
– Housy
Dec 18 '14 at 13:24
2
You don't need a library, just take the input string and convert.
– 2501
Dec 18 '14 at 13:25
3
If you're learning, there's no point in using libraries as there's not much you'll learn. You'll just get the end result directly and things will seem like magic, you wouldn't know how it happened. However, for production code, instead of reinventing the wheel, maintaining the bugs that ensue, using a time-tested library, if that's an option, is the way to go. Even then, you should choose a library and use it with some knowledge on what it does.
– legends2k
Dec 18 '14 at 13:28
1
Here are some excellent pointers.
– bitmask
Dec 18 '14 at 14:24
1
You're already using libc and libm if you haveprintf
,scanf
, andlog
, so what are your actual restrictions, just the stdlib?
– Ryan Haining
Dec 18 '14 at 16:19
5
5
Read the number as a string instead of an
int
. Then process the digits in the string.– Housy
Dec 18 '14 at 13:24
Read the number as a string instead of an
int
. Then process the digits in the string.– Housy
Dec 18 '14 at 13:24
2
2
You don't need a library, just take the input string and convert.
– 2501
Dec 18 '14 at 13:25
You don't need a library, just take the input string and convert.
– 2501
Dec 18 '14 at 13:25
3
3
If you're learning, there's no point in using libraries as there's not much you'll learn. You'll just get the end result directly and things will seem like magic, you wouldn't know how it happened. However, for production code, instead of reinventing the wheel, maintaining the bugs that ensue, using a time-tested library, if that's an option, is the way to go. Even then, you should choose a library and use it with some knowledge on what it does.
– legends2k
Dec 18 '14 at 13:28
If you're learning, there's no point in using libraries as there's not much you'll learn. You'll just get the end result directly and things will seem like magic, you wouldn't know how it happened. However, for production code, instead of reinventing the wheel, maintaining the bugs that ensue, using a time-tested library, if that's an option, is the way to go. Even then, you should choose a library and use it with some knowledge on what it does.
– legends2k
Dec 18 '14 at 13:28
1
1
Here are some excellent pointers.
– bitmask
Dec 18 '14 at 14:24
Here are some excellent pointers.
– bitmask
Dec 18 '14 at 14:24
1
1
You're already using libc and libm if you have
printf
, scanf
, and log
, so what are your actual restrictions, just the stdlib?– Ryan Haining
Dec 18 '14 at 16:19
You're already using libc and libm if you have
printf
, scanf
, and log
, so what are your actual restrictions, just the stdlib?– Ryan Haining
Dec 18 '14 at 16:19
|
show 6 more comments
3 Answers
3
active
oldest
votes
up vote
1
down vote
Since you told that you just need some pointers and not the actual answer, here goes:
I am not able to understand how to convert an String into large Int
That's because you can't. If you want to convert a number that huge to a numerical type, in the first place you need such a type that can hold numbers that big. The language doesn't provide you anything more than long long
which is usually 128-bits long (i.e. if you can use C99, or just long
which is usually lesser than a long long
). Since your tutor told you not to use any external library, it's a clear sign that s/he wants you to code the solution using what's available only in the language and perhaps additionally the standard library.
Is the library function log capable of finding this
No, you can't use stoi
or log
since all of these expect arguments of some arithmetic type, while none of those built-in types are that big to hold numbers this huge. So you've to work completely with strings (i.e. either static or dynamic char buffers).
I understand that you want to use log
to deduce the number of digits the binary output would need; but there's another option, which is to not know the number of digits before hand and allocate them dynamically with some upper bound so that you needn't re-allocate them further.
Lets take an example.
- Allocate 3
char
buffersin
,out
(length of input) andbin
(length of input * 4). - Copy input to
in
- While
in
is not"0"
or"1"
do else goto 12
- For each element
ch
inin
do else goto 10
- Convert
ch
to integeri
- If
is_odd
=1
theni += 10
quot = i / 2
- Append
quot
toout
is_odd = quot % 2
; goto 4
- If
is_odd
=1
append'1'
else'0'
tobin
- Copy
out
toin
, resetout
and goto 3
- Append
in
tobin
- Print
bin
in reverse
When you integer divide a number by 2, the quotient would always be less than or equal to the number of digits of the dividend. So you could allocate in
and out
with the same size as the input and use it for all iterations. For the bin
buffer, the knowledge that each decimal digit wouldn't take more than 4 bits (9 takes a nibble, 1001) would help. So if the input is 10 digits, then 10*4 = 40 bytes would be the upper limit needed for bin
buffer and 10 bytes would be needed for the in
and out
buffers.
This is a vague write-up of an algorithm, I hope it conveys the idea. I feel writing code is more easier than writing algorithms properly.
add a comment |
up vote
0
down vote
I'm afraid there are no standard types in C that will allow you to store such a big value with 20148 bits... You can try to read the string from console (not converting into int), and then parse the string into "010101...." on your own.
The approach would be like that:
You should go for "dividing" the string by 2 in each step (for each division by 2 you need to divide all digits of the string by 2, and handle special cases like 11 / 2 => 5), and for each step if the value cannot be divided by 2, then you then you can put "1" as another binary digit, otherwise you put "0". This way you gather the digits '0', '1', '0', '1', etc. one by one. Then finally you need to reverse the order of digits. A similar approach implemented in C# you can find here: Decimal to binary conversion in c #
add a comment |
up vote
0
down vote
Regarding the update:
Grinding it through WolframAlpha gives:
log(222121212213212313133123413131313131311313154515441315413451315641314563154134156313461316413415635154613415645156451434)
is roughly
274.8056791141317511022806994521207149274321589939103691837589..
Test:
Putting it into exp gives:
2.2212121221321231313312341313131313131131315451544131541.. × 10^119
This raises the question about the precision you need.
Note: I assumed you mean the natural logarithm (base e).
Basically that log is for finding total no. of bits in a decimal no. that we enter. And this is log base 10. I want to do it in my C program, but log cant handle such big numbers.
– user3891236
Dec 19 '14 at 6:06
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Since you told that you just need some pointers and not the actual answer, here goes:
I am not able to understand how to convert an String into large Int
That's because you can't. If you want to convert a number that huge to a numerical type, in the first place you need such a type that can hold numbers that big. The language doesn't provide you anything more than long long
which is usually 128-bits long (i.e. if you can use C99, or just long
which is usually lesser than a long long
). Since your tutor told you not to use any external library, it's a clear sign that s/he wants you to code the solution using what's available only in the language and perhaps additionally the standard library.
Is the library function log capable of finding this
No, you can't use stoi
or log
since all of these expect arguments of some arithmetic type, while none of those built-in types are that big to hold numbers this huge. So you've to work completely with strings (i.e. either static or dynamic char buffers).
I understand that you want to use log
to deduce the number of digits the binary output would need; but there's another option, which is to not know the number of digits before hand and allocate them dynamically with some upper bound so that you needn't re-allocate them further.
Lets take an example.
- Allocate 3
char
buffersin
,out
(length of input) andbin
(length of input * 4). - Copy input to
in
- While
in
is not"0"
or"1"
do else goto 12
- For each element
ch
inin
do else goto 10
- Convert
ch
to integeri
- If
is_odd
=1
theni += 10
quot = i / 2
- Append
quot
toout
is_odd = quot % 2
; goto 4
- If
is_odd
=1
append'1'
else'0'
tobin
- Copy
out
toin
, resetout
and goto 3
- Append
in
tobin
- Print
bin
in reverse
When you integer divide a number by 2, the quotient would always be less than or equal to the number of digits of the dividend. So you could allocate in
and out
with the same size as the input and use it for all iterations. For the bin
buffer, the knowledge that each decimal digit wouldn't take more than 4 bits (9 takes a nibble, 1001) would help. So if the input is 10 digits, then 10*4 = 40 bytes would be the upper limit needed for bin
buffer and 10 bytes would be needed for the in
and out
buffers.
This is a vague write-up of an algorithm, I hope it conveys the idea. I feel writing code is more easier than writing algorithms properly.
add a comment |
up vote
1
down vote
Since you told that you just need some pointers and not the actual answer, here goes:
I am not able to understand how to convert an String into large Int
That's because you can't. If you want to convert a number that huge to a numerical type, in the first place you need such a type that can hold numbers that big. The language doesn't provide you anything more than long long
which is usually 128-bits long (i.e. if you can use C99, or just long
which is usually lesser than a long long
). Since your tutor told you not to use any external library, it's a clear sign that s/he wants you to code the solution using what's available only in the language and perhaps additionally the standard library.
Is the library function log capable of finding this
No, you can't use stoi
or log
since all of these expect arguments of some arithmetic type, while none of those built-in types are that big to hold numbers this huge. So you've to work completely with strings (i.e. either static or dynamic char buffers).
I understand that you want to use log
to deduce the number of digits the binary output would need; but there's another option, which is to not know the number of digits before hand and allocate them dynamically with some upper bound so that you needn't re-allocate them further.
Lets take an example.
- Allocate 3
char
buffersin
,out
(length of input) andbin
(length of input * 4). - Copy input to
in
- While
in
is not"0"
or"1"
do else goto 12
- For each element
ch
inin
do else goto 10
- Convert
ch
to integeri
- If
is_odd
=1
theni += 10
quot = i / 2
- Append
quot
toout
is_odd = quot % 2
; goto 4
- If
is_odd
=1
append'1'
else'0'
tobin
- Copy
out
toin
, resetout
and goto 3
- Append
in
tobin
- Print
bin
in reverse
When you integer divide a number by 2, the quotient would always be less than or equal to the number of digits of the dividend. So you could allocate in
and out
with the same size as the input and use it for all iterations. For the bin
buffer, the knowledge that each decimal digit wouldn't take more than 4 bits (9 takes a nibble, 1001) would help. So if the input is 10 digits, then 10*4 = 40 bytes would be the upper limit needed for bin
buffer and 10 bytes would be needed for the in
and out
buffers.
This is a vague write-up of an algorithm, I hope it conveys the idea. I feel writing code is more easier than writing algorithms properly.
add a comment |
up vote
1
down vote
up vote
1
down vote
Since you told that you just need some pointers and not the actual answer, here goes:
I am not able to understand how to convert an String into large Int
That's because you can't. If you want to convert a number that huge to a numerical type, in the first place you need such a type that can hold numbers that big. The language doesn't provide you anything more than long long
which is usually 128-bits long (i.e. if you can use C99, or just long
which is usually lesser than a long long
). Since your tutor told you not to use any external library, it's a clear sign that s/he wants you to code the solution using what's available only in the language and perhaps additionally the standard library.
Is the library function log capable of finding this
No, you can't use stoi
or log
since all of these expect arguments of some arithmetic type, while none of those built-in types are that big to hold numbers this huge. So you've to work completely with strings (i.e. either static or dynamic char buffers).
I understand that you want to use log
to deduce the number of digits the binary output would need; but there's another option, which is to not know the number of digits before hand and allocate them dynamically with some upper bound so that you needn't re-allocate them further.
Lets take an example.
- Allocate 3
char
buffersin
,out
(length of input) andbin
(length of input * 4). - Copy input to
in
- While
in
is not"0"
or"1"
do else goto 12
- For each element
ch
inin
do else goto 10
- Convert
ch
to integeri
- If
is_odd
=1
theni += 10
quot = i / 2
- Append
quot
toout
is_odd = quot % 2
; goto 4
- If
is_odd
=1
append'1'
else'0'
tobin
- Copy
out
toin
, resetout
and goto 3
- Append
in
tobin
- Print
bin
in reverse
When you integer divide a number by 2, the quotient would always be less than or equal to the number of digits of the dividend. So you could allocate in
and out
with the same size as the input and use it for all iterations. For the bin
buffer, the knowledge that each decimal digit wouldn't take more than 4 bits (9 takes a nibble, 1001) would help. So if the input is 10 digits, then 10*4 = 40 bytes would be the upper limit needed for bin
buffer and 10 bytes would be needed for the in
and out
buffers.
This is a vague write-up of an algorithm, I hope it conveys the idea. I feel writing code is more easier than writing algorithms properly.
Since you told that you just need some pointers and not the actual answer, here goes:
I am not able to understand how to convert an String into large Int
That's because you can't. If you want to convert a number that huge to a numerical type, in the first place you need such a type that can hold numbers that big. The language doesn't provide you anything more than long long
which is usually 128-bits long (i.e. if you can use C99, or just long
which is usually lesser than a long long
). Since your tutor told you not to use any external library, it's a clear sign that s/he wants you to code the solution using what's available only in the language and perhaps additionally the standard library.
Is the library function log capable of finding this
No, you can't use stoi
or log
since all of these expect arguments of some arithmetic type, while none of those built-in types are that big to hold numbers this huge. So you've to work completely with strings (i.e. either static or dynamic char buffers).
I understand that you want to use log
to deduce the number of digits the binary output would need; but there's another option, which is to not know the number of digits before hand and allocate them dynamically with some upper bound so that you needn't re-allocate them further.
Lets take an example.
- Allocate 3
char
buffersin
,out
(length of input) andbin
(length of input * 4). - Copy input to
in
- While
in
is not"0"
or"1"
do else goto 12
- For each element
ch
inin
do else goto 10
- Convert
ch
to integeri
- If
is_odd
=1
theni += 10
quot = i / 2
- Append
quot
toout
is_odd = quot % 2
; goto 4
- If
is_odd
=1
append'1'
else'0'
tobin
- Copy
out
toin
, resetout
and goto 3
- Append
in
tobin
- Print
bin
in reverse
When you integer divide a number by 2, the quotient would always be less than or equal to the number of digits of the dividend. So you could allocate in
and out
with the same size as the input and use it for all iterations. For the bin
buffer, the knowledge that each decimal digit wouldn't take more than 4 bits (9 takes a nibble, 1001) would help. So if the input is 10 digits, then 10*4 = 40 bytes would be the upper limit needed for bin
buffer and 10 bytes would be needed for the in
and out
buffers.
This is a vague write-up of an algorithm, I hope it conveys the idea. I feel writing code is more easier than writing algorithms properly.
edited Dec 18 '14 at 16:44
answered Dec 18 '14 at 15:52
legends2k
18.7k1572151
18.7k1572151
add a comment |
add a comment |
up vote
0
down vote
I'm afraid there are no standard types in C that will allow you to store such a big value with 20148 bits... You can try to read the string from console (not converting into int), and then parse the string into "010101...." on your own.
The approach would be like that:
You should go for "dividing" the string by 2 in each step (for each division by 2 you need to divide all digits of the string by 2, and handle special cases like 11 / 2 => 5), and for each step if the value cannot be divided by 2, then you then you can put "1" as another binary digit, otherwise you put "0". This way you gather the digits '0', '1', '0', '1', etc. one by one. Then finally you need to reverse the order of digits. A similar approach implemented in C# you can find here: Decimal to binary conversion in c #
add a comment |
up vote
0
down vote
I'm afraid there are no standard types in C that will allow you to store such a big value with 20148 bits... You can try to read the string from console (not converting into int), and then parse the string into "010101...." on your own.
The approach would be like that:
You should go for "dividing" the string by 2 in each step (for each division by 2 you need to divide all digits of the string by 2, and handle special cases like 11 / 2 => 5), and for each step if the value cannot be divided by 2, then you then you can put "1" as another binary digit, otherwise you put "0". This way you gather the digits '0', '1', '0', '1', etc. one by one. Then finally you need to reverse the order of digits. A similar approach implemented in C# you can find here: Decimal to binary conversion in c #
add a comment |
up vote
0
down vote
up vote
0
down vote
I'm afraid there are no standard types in C that will allow you to store such a big value with 20148 bits... You can try to read the string from console (not converting into int), and then parse the string into "010101...." on your own.
The approach would be like that:
You should go for "dividing" the string by 2 in each step (for each division by 2 you need to divide all digits of the string by 2, and handle special cases like 11 / 2 => 5), and for each step if the value cannot be divided by 2, then you then you can put "1" as another binary digit, otherwise you put "0". This way you gather the digits '0', '1', '0', '1', etc. one by one. Then finally you need to reverse the order of digits. A similar approach implemented in C# you can find here: Decimal to binary conversion in c #
I'm afraid there are no standard types in C that will allow you to store such a big value with 20148 bits... You can try to read the string from console (not converting into int), and then parse the string into "010101...." on your own.
The approach would be like that:
You should go for "dividing" the string by 2 in each step (for each division by 2 you need to divide all digits of the string by 2, and handle special cases like 11 / 2 => 5), and for each step if the value cannot be divided by 2, then you then you can put "1" as another binary digit, otherwise you put "0". This way you gather the digits '0', '1', '0', '1', etc. one by one. Then finally you need to reverse the order of digits. A similar approach implemented in C# you can find here: Decimal to binary conversion in c #
edited May 23 '17 at 12:32
Community♦
11
11
answered Dec 18 '14 at 13:30
msporek
909415
909415
add a comment |
add a comment |
up vote
0
down vote
Regarding the update:
Grinding it through WolframAlpha gives:
log(222121212213212313133123413131313131311313154515441315413451315641314563154134156313461316413415635154613415645156451434)
is roughly
274.8056791141317511022806994521207149274321589939103691837589..
Test:
Putting it into exp gives:
2.2212121221321231313312341313131313131131315451544131541.. × 10^119
This raises the question about the precision you need.
Note: I assumed you mean the natural logarithm (base e).
Basically that log is for finding total no. of bits in a decimal no. that we enter. And this is log base 10. I want to do it in my C program, but log cant handle such big numbers.
– user3891236
Dec 19 '14 at 6:06
add a comment |
up vote
0
down vote
Regarding the update:
Grinding it through WolframAlpha gives:
log(222121212213212313133123413131313131311313154515441315413451315641314563154134156313461316413415635154613415645156451434)
is roughly
274.8056791141317511022806994521207149274321589939103691837589..
Test:
Putting it into exp gives:
2.2212121221321231313312341313131313131131315451544131541.. × 10^119
This raises the question about the precision you need.
Note: I assumed you mean the natural logarithm (base e).
Basically that log is for finding total no. of bits in a decimal no. that we enter. And this is log base 10. I want to do it in my C program, but log cant handle such big numbers.
– user3891236
Dec 19 '14 at 6:06
add a comment |
up vote
0
down vote
up vote
0
down vote
Regarding the update:
Grinding it through WolframAlpha gives:
log(222121212213212313133123413131313131311313154515441315413451315641314563154134156313461316413415635154613415645156451434)
is roughly
274.8056791141317511022806994521207149274321589939103691837589..
Test:
Putting it into exp gives:
2.2212121221321231313312341313131313131131315451544131541.. × 10^119
This raises the question about the precision you need.
Note: I assumed you mean the natural logarithm (base e).
Regarding the update:
Grinding it through WolframAlpha gives:
log(222121212213212313133123413131313131311313154515441315413451315641314563154134156313461316413415635154613415645156451434)
is roughly
274.8056791141317511022806994521207149274321589939103691837589..
Test:
Putting it into exp gives:
2.2212121221321231313312341313131313131131315451544131541.. × 10^119
This raises the question about the precision you need.
Note: I assumed you mean the natural logarithm (base e).
edited Dec 18 '14 at 16:22
answered Dec 18 '14 at 16:16
mvw
4,03711827
4,03711827
Basically that log is for finding total no. of bits in a decimal no. that we enter. And this is log base 10. I want to do it in my C program, but log cant handle such big numbers.
– user3891236
Dec 19 '14 at 6:06
add a comment |
Basically that log is for finding total no. of bits in a decimal no. that we enter. And this is log base 10. I want to do it in my C program, but log cant handle such big numbers.
– user3891236
Dec 19 '14 at 6:06
Basically that log is for finding total no. of bits in a decimal no. that we enter. And this is log base 10. I want to do it in my C program, but log cant handle such big numbers.
– user3891236
Dec 19 '14 at 6:06
Basically that log is for finding total no. of bits in a decimal no. that we enter. And this is log base 10. I want to do it in my C program, but log cant handle such big numbers.
– user3891236
Dec 19 '14 at 6:06
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f27547605%2fc-converting-a-very-large-integer-into-a-binary%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
Read the number as a string instead of an
int
. Then process the digits in the string.– Housy
Dec 18 '14 at 13:24
2
You don't need a library, just take the input string and convert.
– 2501
Dec 18 '14 at 13:25
3
If you're learning, there's no point in using libraries as there's not much you'll learn. You'll just get the end result directly and things will seem like magic, you wouldn't know how it happened. However, for production code, instead of reinventing the wheel, maintaining the bugs that ensue, using a time-tested library, if that's an option, is the way to go. Even then, you should choose a library and use it with some knowledge on what it does.
– legends2k
Dec 18 '14 at 13:28
1
Here are some excellent pointers.
– bitmask
Dec 18 '14 at 14:24
1
You're already using libc and libm if you have
printf
,scanf
, andlog
, so what are your actual restrictions, just the stdlib?– Ryan Haining
Dec 18 '14 at 16:19