GATE Computer ScienceHardGATE CS
Assume a 32-bit unsigned int. What does the following program print? #include <stdio.h> int main() { unsigned s = 0; for (unsigned i = 1; i <= 1000; i++) s += i & -i; printf("%u", s); }
- A. 5050
- B. 4950
- C. 5060
- D. 5120
Correct answer: C. 5060
For unsigned i, −i wraps modulo 2³², so i & −i isolates the lowest set bit of i. Summing the lowest set bit for i = 1 to 1000 gives 5060: each power 2ᵏ is the lowest bit of the numbers 2ᵏ·(odd) up to 1000 and contributes 2ᵏ for each of them.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- Assume uint16t is a 16-bit unsigned type, int is 32 bits and the characters are ASCII. What is printed? uint16t h = 7; for (const char p =…
- int add(int a, int b) { return a + b; } int sub(int a, int b) { return a - b; } int mul(int a, int b) { return a b; } int main() { int…
- Hoare partition on a[0..8] = {8, 3, 9, 1, 8, 5, 2, 7, 6}: int hoare(int a[], int lo, int hi) { int p = a[lo], i = lo - 1, j = hi + 1…
- A singly linked list has a tail of 6 nodes leading into a cycle of 9 nodes. A pointer 'slow' moves 1 node per step and a pointer 'fast'…
- An open-addressing table of size 11 uses h(k) = k mod 11 with linear probing and tombstones. Insertion places the key in the first empty…
- A sparse table for range-minimum queries on an array of 1000 elements stores, for each level k = 0, 1, ..., ⌊log₂ 1000⌋, one entry for…