Pushok icon

bit fields

Pushok | PRO | 06/12/12 10:59:56 AM UTC | 0 ⭐ | 348 👁️ | Never ⏰ | []
C++ |

420 B

|

None

|

0 👍

/

0 👎

#include <iostream>
 
#pragma pack(push, 1)
struct test
{
    bool b1:1;
    bool b2:1;
    bool b3:1;
    bool b4:1;
 
    bool b5:1;
    bool b6:1;
    bool b7:1;
    bool b8:1;
};
#pragma pack(pop)
 
int main()
{
    test t;
 
    *(bool*)(&t) = 0;
 
    t.b1 = 1;
    t.b2 = 0;
    t.b3 = 1;
    t.b4 = 0;
    t.b5 = 1;
    t.b6 = 0;
    t.b7 = 1;
    t.b8 = 0;
 
    std::cout << t.b1 << " " << t.b2 << " " << t.b3 << " " << t.b4; 
 
    std::cin.get();
}

Comments