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).