ProgrammingVery hardProduct company OA · GATE CS
With a 32-bit unsigned int x, which expression has undefined behaviour?
- A. (x << 16) << 16
- B. x << 32
- C. x << 0
- D. x >> 31
Correct answer: B. x << 32
Shifting by a count greater than or equal to the width of the (promoted) type is undefined, even for unsigned values; on x86 x << 32 often gives x unchanged rather than 0. Two shifts of 16 are each legal and give 0.
More C Programming questions
- What is printed? c char s[] = "7392"; int n = 0; for (int i = 0; s[i]; i++) n = n 10 + (s[i] - '0'); printf("%d %d", n % 1000, n / 100 %…
- What is printed? c int f(int n) { if (n == 0) return 0; return (n & 1) + f(n >> 1) 10; } int main(void) { printf("%d", f(13)); }
- What is printed? c struct S { int a; int b; } s[] = {{1, 10}, {2, 20}, {3, 30}}; struct S p = s; int x = ++p->a; int y = (++p)->b; int z =…
- Assume 32-bit unsigned int. What is printed? c unsigned x = 0x12345678; printf("%X", (x >> 16) | (x << 16));
- What is printed? c int ok(const char s, int d) { if (d < 0) return 0; if (!s) return d == 0; return ok(s + 1, d + (s == '(') - (s ==…
- What is printed? c int n = 1200, r = 0; while (n) { r = r 10 + n % 10; n /= 10; } printf("%d", r);