Displaying data as binary in C with bitmasks I wrote a program to display data in memory as binary and it worked. (It gave me immense sense of satisfaction and joy to see this work. I guess this happens to newbies who are just testing the waters). I know this isn't some terribly smart code, and I'm sure there must be surely lots of better ways to do this. Any advice and help would be great. If I've done something wrong in this program or if there is any bad programming practice, I would be happy to be corrected.

#include&lt;stdio.h&gt;

int main()
{

  char mychar = 'a';
  int count = 0;
  int bitmask = 128;
  while (count &lt; 8){

    if (mychar &amp;  bitmask){
      printf("1");
    }
    else{
       printf("0");
    }

    bitmask = bitmask / 2;
    count = count +1;
  }

return 0;

}
You may use the shift operation;

char mask = 0x01; //define initial mask

if (mask&amp;mychar) printf("1"); // check that spesific bit

mask&lt;&lt;=1; //shift left


of course if you want to start checking from right hand then shift right you can do :

char mask = 0x80;

mask&gt;&gt;=1;