Bitfield using 1 byte instead of 1 bit

  bit-fields, c++

I am working on a networking application where I will receive 2 bytes and certain bits have specific significance. I am trying to implement that packet as a structure. The intent is to do a binary copy to object address and the fields of the packet are ready to be accessed. Here is a simple example representing my problem. When we try to inspect the size of the bitfield and structure they are not coming as expected.

#include <bitset>
#include<iostream>
struct a
    {
        std::bitset<8> b;
        uint8_t c;

    };
int main()
{
    std::cout<<sizeof(a);
}

Output: 8

Expected Output: 2

Is this something specific to Bitset’s implementation?

Generally, each element occupies only one bit (which, on most systems, is eight times less than the smallest elemental type: char).
(ref- cplusplus.com/reference/bitset/bitset/ )

compiled on Microsoft Visual Studio 2019 16.10.2

Source: Windows Questions C++

LEAVE A COMMENT