I needed to read the value of single bits out of a char variable. This is how I've done it - please doublecheck if this is correct (and for sure not the best/fastest way of doing it).
1) Create first of all a structure of bits
struct BITS { bool b1:1; bool b2:1; bool b3:1; bool b4:1; bool b5:1; bool b6:1; bool b7:1; bool b8:1; };
2) Now declare the variables in the function you're using:
void myfunction() { char cTarget; BITS b;
3) Now assign a value to char and fill the BITS structure:
cout << "The full byte has a meaning of...: " << cByteRead << endl; cout << "...which is in int:" << (int)cByteRead << endl; b = *(BITS*)(&cByteRead);
4) And finally display the values of the bits:
cout << b.b1 << endl; cout << b.b2 << endl; cout << b.b3 << endl; cout << b.b4 << endl; cout << b.b5 << endl; cout << b.b6 << endl; cout << b.b7 << endl; cout << b.b8 << endl;
Everything altogether:
struct BITS { bool b1 :1; bool b2 :1; bool b3 :1; bool b4 :1; bool b5 :1; bool b6 :1; bool b7 :1; bool b8 :1; }; void myfunction() { char cByteRead; BITS b; cByteRead = 'a'; cout << "The full byte has a meaning of...: " << cByteRead << endl; cout << "...which is in int:" << (int)cByteRead << endl; b = *(BITS*)(&cByteRead); cout << b.b1 << endl; cout << b.b2 << endl; cout << b.b3 << endl; cout << b.b4 << endl; cout << b.b5 << endl; cout << b.b6 << endl; cout << b.b7 << endl; cout << b.b8 << endl; }
The mapping of the integer value of a char variable is:
Bit8 Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Int 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0 1 | 1 0 0 0 0 0 0 1 0 | 2 0 0 0 0 0 0 1 1 | 3 0 0 0 0 0 1 0 0 | 4 0 0 0 0 0 1 0 1 | 5 0 0 0 0 0 1 1 0 | 6 0 0 0 0 0 1 1 1 | 7 0 0 0 0 1 0 0 0 | 8 0 0 0 0 1 0 0 1 | 9 0 0 0 0 1 0 1 0 | 10 0 0 0 0 1 0 1 1 | 11 0 0 0 0 1 1 0 0 | 12 0 0 0 0 1 1 0 1 | 13 0 0 0 0 1 1 1 0 | 14 0 0 0 0 1 1 1 1 |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 0 | 220 1 1 0 1 1 1 0 1 | 221 1 1 0 1 1 1 1 0 | 222 1 1 0 1 1 1 1 1 | 223 1 1 1 0 0 0 0 0 | 224 1 1 1 0 0 0 0 1 | 225 1 1 1 0 0 0 1 0 | 226 1 1 1 0 0 0 1 1 | 227 1 1 1 0 0 1 0 0 | 228 1 1 1 0 0 1 0 1 | 229 1 1 1 0 0 1 1 0 | 230 1 1 1 0 0 1 1 1 | 231 1 1 1 0 1 0 0 0 | 232 1 1 1 0 1 0 0 1 | 233 1 1 1 0 1 0 1 0 | 234 1 1 1 0 1 0 1 1 | 235 1 1 1 0 1 1 0 0 | 236 1 1 1 0 1 1 0 1 | 237 1 1 1 0 1 1 1 0 | 238 1 1 1 0 1 1 1 1 | 239 1 1 1 1 0 0 0 0 | 240 1 1 1 1 0 0 0 1 | 241 1 1 1 1 0 0 1 0 | 242 1 1 1 1 0 0 1 1 | 243 1 1 1 1 0 1 0 0 | 244 1 1 1 1 0 1 0 1 | 245 1 1 1 1 0 1 1 0 | 246 1 1 1 1 0 1 1 1 | 247 1 1 1 1 1 0 0 0 | 248 1 1 1 1 1 0 0 1 | 249 1 1 1 1 1 0 1 0 | 250 1 1 1 1 1 0 1 1 | 251 1 1 1 1 1 1 0 0 | 252 1 1 1 1 1 1 0 1 | 253 1 1 1 1 1 1 1 0 | 254 1 1 1 1 1 1 1 1 | 255