Code:
#include<stdio.h>
void printbits(unsigned int tobeprinted)
{
unsigned int shift=8*sizeof(unsigned int)-1; // bits are 0-31 not 1-32
unsigned int mask=1<<shift;
printf("%i is ",tobeprinted);
for(unsigned int i=1;i<=(shift+1);i++)
{
if(tobeprinted &mask) printf("1");
else
printf("0");
tobeprinted <<= 1;
if (i%8==0) printf(" ");
}
}
int main()
{
for (int i=1;i<10;i++)
{
printf("Enter positive integer :-");
unsigned int input;
scanf("%i",&input);
printbits(input);
printf("\n");
}
return 0;
}
not exactly what you are after but it shows one possible method of extracting bits from an int.From this you will be able to do the same for a char.