As a practice exercise I'm trying to convert an entered char value to binary and display each bit of the 8 bit output. I have found some example code for conversion:
int main (void)
{
char a =16;
int i=0;
for (i = 0; i < 8;i++) {
printf("%d", !!((a << i) & 0x80));}
return 0;
}
however when I use an entered value for a such as with the code:
int main(void){
char a;
printf("Enter char value:", a);
scanf("%c", &a);
printf("a=%c", a);
return 0 ;}
the incorrect value is given. And also how would I be able to display each bit separately? for example
printf("The sign bit is %d\n", &);
printf("The bit 6 is %d\n", &);
printf("The bit 5 is %d\n", &);
printf("The bit 4 is %d\n", &);
printf("The bit 3 is %d\n", &);
printf("The bit 2 is %d\n", &);
printf("The bit 1 is %d\n", &);
printf("The bit 0 is %d\n", &);