This function not working for 4095.
long long convert(int decimalnum)
{
long long binarynum = 0;
int rem, temp = 1;
while (decimalnum!=0)
{
rem = decimalnum%2;
decimalnum = decimalnum / 2;
binarynum = binarynum + rem*temp;
temp = temp * 10;
}
return binarynum;
}
printf("%lld", convert(4095));
result is:-558038585
Is there a way to make the function work for this number?
Notice that temp will exceeds the range of possible values for an integer number. You should fix your code as follows:
long long convert(int decimalnum)
{
long long binaryNum = 0;
long long temp = 1;
int rem = 1;
.
.
.
}
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.