ProgrammingVery hardProduct company OA
Assume 32-bit unsigned int and 64-bit unsigned long long. What is printed?
Assume 32-bit unsigned int and 64-bit unsigned long long. What is printed?
unsigned int a = 0x80000001u;
unsigned int b = a * 2;
int c = (int)(a >> 31);
unsigned int d = (a << 1) >> 1;
unsigned long long e = (unsigned long long)a << 1;
printf("%u %d %u %llu", b, c, d, e);- A. 2 1 2147483649 4294967298
- B. 2 -1 1 2
- C. 2 1 1 4294967298
- D. 4294967298 1 1 4294967298
Correct answer: C. 2 1 1 4294967298
Unsigned arithmetic is modulo 2³²: a·2 = 0x100000002 → 2. a >> 31 on an unsigned value is a logical shift → 1. (a << 1) already lost the top bit (it is evaluated in 32 bits) so shifting back gives 1, not the original a. Only when a is widened BEFORE the shift does the carry survive: 4294967298. The option with d = 2147483649 assumes the compiler 'remembers' the bit shifted out.
More C Programming questions
- Assume a 64-bit LP64 ABI (4-byte int, 8-byte pointers and double, natural alignment). What is printed? c char s[] = "a0bc"; char p = s…
- What is printed? c int inc(int x) { return x + 1; } int dbl(int x) { return x 2; } int sqr(int x) { return x x; } int apply(int…
- What is printed? c int s = 0; for (int i = 0; i < 6; i++) { switch (i % 4) { case 0: s += 1; case 1: s += 2; break; case 2: continue…
- What is printed? c int calls; int f(int n) { calls++; return n < 3 ? n : f(n - 1) + f(n - 3); } int main(void) { int r = f(8); printf("%d…
- What is printed? c char s[] = "aaabccdddd"; char r = s, w = s; while (r) { char c = r; int n = 0; while (r == c) { r++; n++; } w++ = c; if…
- Assume 32-bit unsigned int. What is printed? c unsigned int x = 0x5Cu; / 0101 1100 / unsigned int low = x & -x; unsigned int y = x + low…