ProgrammingVery hardProduct company OA
What is printed?
What is printed?
struct { unsigned a : 3; unsigned b : 5; } f = {0, 0};
f.a = 9;
f.b = 33;
printf("%u %u ", f.a, f.b);
f.a += 7;
f.b = f.b - 2;
printf("%u %u", f.a, f.b);- A. 1 1 0 31
- B. 9 33 16 31
- C. 1 1 0 0
- D. 1 1 8 31
Correct answer: A. 1 1 0 31
An unsigned bit-field of width w stores values modulo 2^w, exactly like an unsigned integer of that width. a has 3 bits: 9 mod 8 = 1, then 1 + 7 = 8 mod 8 = 0. b has 5 bits: 33 mod 32 = 1; f.b − 2 is computed in int as −1, and converting −1 to a 5-bit unsigned field gives 31. Nothing here is undefined: unsigned wrap-around is defined.
More C Programming questions
- What is printed? c / 32-bit unsigned int / unsigned np2(unsigned v) { v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >>…
- What is printed? c / 8-bit unsigned char, 32-bit int / unsigned char c = 0x80; printf("%d %d %d %d", c << 1, (unsigned char)(c << 1), ~c…
- What is printed? c char s[] = "a,b,,c"; int n = 0; for (char t = strtok(s, ","); t; t = strtok(NULL, ",")) n++; printf("%d %zu %c", n…
- What is printed? c char s[] = "Hello"; s[0] = 'J'; s[4] = '0'; printf("%s %zu", s, sizeof(s));
- What is printed? c int i = 5; printf("%d ", i++); printf("%d", ++i);
- What is printed? c printf("%.2f %d %d", 7 / 2.0, 7 / 2, 7 % 2);