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

后端 未结 3 1735
闹比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:16

    If not, why so? If bit-fields are basically intended to use less memory, why can't we >declare any variable as a bit-field if we know it won't exceed that size?

    Because the memory is fixed at 16 or 32 bits wide. It would be too slow to pack a users 14 bit ints into 16 bits and use the other 2 bits . Its better to waste the 2 bits and don't let you define in efficient storage. Thats why if you really want to you can use a union. You wouldn't be gaining anything by making the compiler work harder for nothing. The people who wrote the compiler were smart. They don't just let a programmer with a bad idea slow things down, sorry/.

提交回复
热议问题