Question

The output of printBits('A') suppose to be 0100 0001 but I'm getiing 0100 0001 0100 1101 0000 0000. I can't seem to figure out the problem in my code below.

unsigned char getBit(unsigned char c, int n) {
  return c=(c&(1<<n))>>n;

}


void printBits(unsigned char c) {
  int i=7;
  while(i>=0){
    printf("%d", (getBit(c,i--);
  }
}
Was it helpful?

Solution

Your output seems to match your input just fine, but your output shows you called your print function two more times. The output matches this sequence of calls:

printBits('A');
printBits('M');
printBits('\0');

This is shown here (after fixing a syntax error in your print function).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top