ProgrammingVery hardProduct company OA
Assume 32-bit unsigned int. What is printed?
Assume 32-bit unsigned int. What is printed?
unsigned int x = 0x5Cu; /* 0101 1100 */
unsigned int low = x & -x;
unsigned int y = x + low;
unsigned int z = y | (((x ^ y) / low) >> 2);
printf("%u %u %u", low, y, z);- A. 4 96 111
- B. 4 96 99
- C. 4 96 96
- D. 2 94 95
Correct answer: B. 4 96 99
x & −x isolates the lowest set bit: 4. Adding it ripples through the lowest block of ones (111 at bits 2–4) and sets the next zero: y = 0110 0000 = 96. x ^ y = 0011 1100 marks the changed bits; dividing by low right-aligns it (1111) and >> 2 drops two, leaving 11 – the block's remaining ones moved to the bottom. z = 0110 0011 = 99: the next larger integer with the same number of 1-bits (Gosper's hack). 111 forgets the >> 2 and gains a bit.
More C Programming questions
- Assume 4-byte int with 4-byte alignment. What is printed? c struct P { char tag; int v; }; struct P a[4] = {{'a', 1}, {'b', 2}, {'c', 3}…
- Assume plain char is signed 8-bit, two's complement, and EOF is −1. What is printed? c char c = 'xff'; printf("%d %d %d %d", c == 0xFF…
- Read the declaration: c char ((x)[4])(int); What is x?
- What is printed? c char a[8] = "abcdefg"; memmove(a + 2, a, 4); char b[6] = "XXXXX"; strncpy(b, "hi", 4); char c[4]; strncpy(c, "wxyz"…
- What is printed? c struct N { int v; struct N next; }; struct N n[5] = {{1, n + 1}, {2, n + 2}, {3, n + 3}, {4, n + 4}, {5, 0}}; struct N…
- What is printed? c int lb(const int a, int n, int x) { int lo = 0, hi = n; while (lo < hi) { int mid = lo + (hi - lo) / 2; if (a[mid] < x)…