C++ - How to use bitfields

Viewed 13436

So I recently came across something like this

unsigned char ch : 7;

inside a struct. I read about it a little. Apparently these are called bit fields. They are used to set the width of data that a char can receive. But how do we use these things. For example, I know that we can set variable ch to be a byte

unsigned char ch = 0x61;
cout << ch << endl;

This will output

a

However, what do we do with the bitfields?

unsigned char ch : 7;
ch = 0x61;                //This doesn't work for some reason

unsigned char ch : 7;
unsigned char ch = 0x61/  //Neither does this.

Thanks for the help

1 Answers
Related