GATE Computer ScienceHardGATE CS (PYQ pattern)
unsigned f(unsigned x) { unsigned c = 0; while (x) { x &= x - 1; c++; } return c; } unsigned g(unsigned x) { return x & ~(x - 1); } unsigned h(unsigned x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; return x - (x >> 1); } What is f(0xB4) * 100 + g(0xB4) + h(0xB4)?
- A. 659
- B. 704
- C. 532
- D. 660
Correct answer: C. 532
0xB4 = 1011 0100. f clears the lowest set bit each time, so it counts ones: 4. x & ~(x - 1) equals x & -x, the lowest set bit: 4. h smears the top bit rightwards (giving 1111 1111 = 255 for any byte-sized x) and x - (x >> 1) keeps only the highest set bit: 128. Total 400 + 4 + 128 = 532.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- int i = 2, j = 3, k, m; k = i += j, j -= i, i j; m = (i, j); printf("%d %d %d %d", i, j, k, m); What is printed?
- int a[] = {10, 20, 30, 40}; int p[] = {a, a + 1, a + 2, a + 3}; int pp = p; int t1 = pp++; int t2 = ++pp; int t3 = ++++pp; printf("%d %d…
- Assume char is 1 byte, int is 4 bytes with 4-byte alignment, double is 8 bytes with 8-byte alignment, and the compiler inserts only the…
- int cnt = 0; int f(int n) { cnt++; if (n <= 1) return 1; if (n == 2) return f(1) + 1; return f(n - 1) + f(n - 2) + f(n - 3); } After f(6)…
- int g(int n) { static int s = 0; s += n; if (n > 0) { g(n - 1); s += n; } return s; } int main() { int x = g(3); int y = g(2); printf("%d…
- void p(int n) { if (n == 0) return; printf("%d ", n); p(n - 1); printf("%d ", n 10); if (n % 2 == 0 && n > 2) p(n - 2); } How many numbers…