You are printing the character who's value is 7. Others have pointed out that this is a special character that is usually not displayed. Cast your value to int
or another non-char integer type to display the value, rather than the character. Go look at the ascii table and you'll see character 7 is BEL (bell).
#include <iostream>
using namespace std;
struct bitfield
{
unsigned char a : 3, b : 3;
};
int main()
{
bitfield bf;
bf.a = 7;
cout << (int)bf.a; // Added (int) here
char c;
cin >> c;
return 0;
}
Edit 1: Since bf.a
is only 3 bits, it cannot be set to any displayable character values. If you increase it's size, you can display characters. Setting it to 46 gives the period character.
#include <iostream>
using namespace std;
struct bitfield
{
unsigned char a : 6, b : 2;
};
int main()
{
bitfield bf;
bf.a = 46;
cout << bf.a;
char c;
cin >> c;
return 0;
}
Edit 2: See This answer about using bitfields and char
.