Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
char *iToBin(int value)
{
char *string = calloc(sizeof(int) * 8 + 1, sizeof(char));
int i;
for (i = 0; i < sizeof(int) * 8; i++)
string[i] = (value & (1 << i)) ? '1' : '0';
string[i] = '\0';
return string;
}
#include <stdio.h>
#include <limits.h>
/*
* Note use of static store - the result must be used before calling
* this function again.
* This for example is wrong
* printf( "Result=%s %s\n", iToBin(0x0), iToBin(0xff) );
*/
char *iToBin( unsigned int val )
{
static char result[CHAR_BIT*sizeof(unsigned int)+1];
unsigned int mask = 1u << (CHAR_BIT*sizeof(unsigned int)-1);
char *p = result;
do
{
*p++ = val & mask ? '1' : '0';
} while ( mask >>= 1 );
*p = '\0';
return result;
}
int main ( ) {
printf( "Result=%s\n", iToBin(0x0u) );
printf( "Result=%s\n", iToBin(0x55u) );
printf( "Result=%s\n", iToBin(0xAAu) );
printf( "Result=%s\n", iToBin(0x12345678u) );
printf( "Result=%s\n", iToBin(0x87654321u) );
return 0;
}