#include <stdio.h>
char *toBinFormat(int n);
int main(int argc, char **argv)
{
// flip/invert (~)
// result bits invert. turning a 1 into a 0 and a 0 into a 1
char tmp;
tmp = 7;
printf("%3d = %s\n", tmp, toBinFormat(tmp));
tmp = ~tmp;
printf("%3d = %s\n", tmp, toBinFormat(tmp));
return(0);
}
char *toBinFormat(int n)
{
static char binary_value[9]; // without static binary_value is a local
// array that disappear after return
int i;
for(i = 0; i < 8; i++)
{
binary_value[i] = n & 0x80 ? '1' : '0';
n <<= 1;
}
binary_value[i] = '\0';
return binary_value;
}
/*
run:
7 = 00000111
-8 = 11111000
*/