I am writing a program where I have to display binary representations of various data types. I need the binary output to have a space after every four numbers. For instance:
0011 1111 1000 1110 1011 1000 0101 0010
Below is sample of a function I am using to display the binary code. What is the best way to format the output with the spaces?
void printChar(char testChar)
{
unsigned char mask = pow(2, ((sizeof(char) * 8) - 1));
cout << "The binary representation of " << testChar << " is ";
for (int count = 7; count >= 0; count--)
{
if ((testChar & mask) != 0)
{
cout << "1";
}
else
{
cout << "0";
}
mask = (mask >> 1);
}
cout << endl;
}