struct F { unsigned flag : 3; }; is declared. What range can flag hold, and what happens when 8 is assigned?
Correct answer
B. 0 to 7, with 8 stored as 0 because the value is kept modulo 8 by the field
17 views
B. 0 to 7, with 8 stored as 0 because the value is kept modulo 8 by the field
17 views
Bit-fields of n unsigned bits hold 0 to 2^n - 1, so a three-bit field holds 0 to 7 and assigning 8 keeps the low three bits, giving 0. A plain int bit-field is implementation-defined as signed or unsigned, which is why unsigned should be written explicitly. Field ordering and packing are also implementation-defined, so bit-fields must not be used for binary interchange formats.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is B? Ask here and get an explanation.