I have debugged and dec2bin function outputs "bin" value is right, but cout function somehow prints out wrong value. How to fix it? Thanks!
#include <iostream>
#define bit 5
void dec2bin (int, int *);
int main (void)
{
int bin[bit];
int num = 24; // example
dec2bin(num, bin);
for (int i=0; i<bit; i++)
{
std::cout << bin[0]; // should be 11000
}
getchar();
return 0;
}
void dec2bin (int num, int *bin)
{
int i = 0;
while ((num != 0) && (i < bit))
{
bin[bit- 1 - i ] = num % 2;
num = num / 2;
i++;
}
}