Can bit-fields only be fields of a structure/union, never “normal”, “stand-alone” variables?

后端 未结 3 1724
闹比i
闹比i 2020-12-21 09:40

The field part of bit-fields seems to suggest that they can only be fields inside a structure or union.

Can a bit-field be a typical \"stand-alone\" variable, outsid

3条回答
  •  礼貌的吻别
    2020-12-21 10:18

    Bit fields can only be defined in structs and unions where they can be referred to individually by name. You cannot address memory by bits, a minimum of a byte size is required (8 bits). In order to address a variable bit by bit, you may use a mask like so:

    int num = 9;
    int fourthBit = (num >> 4) & 1;
    

    A struct can have a bigger size, for example an int (of 4 bytes) and then be divided, by bits, to different pieces. Of course, the assignment will be compiled using masks.

    Refer to this for more info: http://msdn.microsoft.com/en-us/library/yszfawxh(v=vs.80).aspx

提交回复
热议问题