ProgrammingVery hardGATE CS · Campus placements
A uint32_t x = 37 is rounded up to a power of two by this code: x--; x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; x++; What is x?
- A. 37
- B. 40
- C. 64
- D. 32
Correct answer: C. 64
After x-- the value is 36 = 100100. Smearing sets every bit below the top set bit, giving 63, and adding one gives 64. The x-- at the start is what makes exact powers of two map to themselves.
More C Programming questions
- int a = -7, m = 5; What is ((a % m) + m) % m, the non-negative residue of a modulo m?
- unsigned char c = 200; int r = (c c) >> 8; What is r?
- unsigned char c = 0xF0; printf("%d", ~c); prints what?
- int x = 7; int a = &x, b = &x; a ^= b; b ^= a; a ^= b; What is x afterwards?
- int a[] = {5, 1, 4}; int p = a; p++ += 10; printf("%d %d", a[0], p); prints what?
- struct P { int id; int score; }; An array must be sorted by score descending, with ties broken by id ascending, using qsort. Which…