Can bit fields be structure / union fields, never “normal”, “autonomous” variables?

The field portion of bit fields seems to suggest that they can only be fields within a structure or union.

Can a bit field be a typical "autonomous" variable, outside of any aggregate data type, such as a union or structure, as follows:

   int sum:6;   //Can this work as a declaration statement?
   sum=4;

If not, why? If bit fields are mainly designed to use less memory, why can't we declare any variable as a bit field if we know that it will not exceed this size?

+2
source share
3 answers

- , < . -. , , .

C99:

:

6.7

6.7.2 (, )

6.7.2.1 .

declarator_opt : constant-expression . , , - .

, , . 6.7.2.1:3 - 0 .

+3

- , . , (8 ). , ​​:

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

, , int ( 4 ), . , .

: http://msdn.microsoft.com/en-us/library/yszfawxh(v=vs.80).aspx

0

, ? - , - -, , ?

16 32 . , 14- ints 16 2 . 2 . , , .   , . , , . , /.

0

Source: https://habr.com/ru/post/1673463/


All Articles