I want to convert a unsigned integer into an array of 48 bit binary representation,if the result is less than 48 bit then I need to zero out the rest of the bits. Here's the function:
char *getBinary(unsigned int num)
{
char* bstring;
int i;
bstring = (char*) malloc(sizeof(char) * 49);
assert(bstring != NULL);
bstring[48] = '\0';
for( i = 0; i < 48; i++ )
{
bstring[48 - 1 - i] = (num == ((1 << i) | num)) ? '1' : '0';
}
return bstring;
}
And in the main function:
unsigned int nbr1=3213816984;
char * bi=getBinary(nbr1);
The result should be:
000000000000000010111111100011101111010010011000
But instead I got:
111101001001100010111111100011101111010010011000
Why I can't zero out the bit that's more than 32 bits?