for (n = 0; pow(2, n) < decimal; n++) {
temp = (int)pow(2, n);
}
n -= 1;
If the number of bits needed should be calculated here, then subtracting one here would not be correct. With negative numbers, n=0 would not work.Since negative numbers are binary based on the two's complement, this approach would not lead to a solution. The number of bits can be calculated without repeatedly calculating pow in a loop, or is the function log not allowed?
char binar[65];
Why is the array so big? Are there computers where an int has so many bits?
If you don't know, I would recommend allocating the memory in the necessary size.
if (temp < decimal) {
binar[0] = 1;
}
The query is always fulfilled with some certainty if decimal is not 0. In the most significant position binary[0] there is then (of course) a 1.
After that it just goes wrong, as you would certainly notice in the debugger.
for (i = n - 1, j = 1; j <= 7; i--, j++)
{
if (temp + pow(2, i) < decimal) {
binar[j] = 1;
temp = temp + (int)pow(2, i);
}
else {
binar[j] = 0;
}
}
The loop continues even with i=-1 ...
If you simply shifted the decimal value and masked out the lowest digit, you could save yourself all that. And the result would be easier and also more correct.