ProgrammingHardWipro · Cognizant pattern

Assume a 64-bit LP64 ABI. What is printed?

Assume a 64-bit LP64 ABI. What is printed?
struct Flags {
    unsigned isActive : 1;
    unsigned isAdmin : 1;
    unsigned level : 6;
};
struct Flags f = {1, 0, 42};
printf("%u %u %u", f.isActive, f.isAdmin, f.level);
  1. A. 1 0 0
  2. B. 1 0 42
  3. C. 1 1 42
  4. D. 0 0 42

Correct answer: B. 1 0 42

Each bitfield independently stores its initializer value, clipped to its declared width: isActive(1 bit) holds 1, isAdmin(1 bit) holds 0, level(6 bits, range 0-63) holds 42 exactly (fits within 6 bits), so reading them back gives exactly what was assigned: 1 0 42.

More C Programming questions

Exams that ask this topic