GATE / CS ARCHIVE

SIMILAR QUESTIONS / PROGRAMMING IN C

Count integer halving and invert nested logarithm thresholds

3 questions solved the same way, newest first. Back to Programming in C

How these are solved

Derive direct or two-stage logarithmic counts from doubling, right shifts or repeated halving. Preserve loop comparison versus body-execution counts, bit-length and termination boundaries.

2 marks · MCQ
Report issue

Two integer-halving loops

Control Flow & Loop Tracing

Question 32, GATE 2018: A 64-bit unsigned-long function initializes j=sum=0. It runs for(i=n;i>1;i=i/2) j++; then for(;j>1;j=j/2) sum++; returns sum. For n=2^40, choose A 4; B 5; C 6; D 40.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Method

The first loop sees i=2^40,2^39,…,2 before stopping at i=1, so its body runs 40 times. The second loop then uses integer division: 40→20→10→5→2→1. It executes for the first five of these values, not for the final 1. The two loops are consecutive, not nested: multiplying their iteration counts would not describe the return value. The exponent in 2^40 is essential and can be lost in text extraction.

Solution

The first loop halves i until 1, performing 40 iterations, hence j=40. The second loop has j values 40, 20, 10, 5, 2 before stopping at 1, so sum=5, B.

Original paper, page 13 · Answer key

1 mark · NAT
Report issue

Control Flow & Loop Tracing

Question 10, GATE 2014: For the C function int func(int num){int count=0; while(num){count++; num>>=1;} return count;}, find func(435).
Tap to enlarge
Answer & solution

Official answer: 9

1. Set up the reasoning. The input is positive, so repeated right shifts discard low bits until the most significant 1 has also been shifted away. The return value is binary length, not population count.

2. Derive the result. For positive num, each right shift divides by 2 and discards the remainder. The sequence 435, 217, 108, 54, 27, 13, 6, 3, 1 has 9 nonzero values before 0, so the loop returns 9.

3. Check the interpretation. Since 28≤435<29, exactly nine shifts are needed. This gives a compact independent check of the explicit sequence and avoids confusing shifts-to-zero with the number of set bits.

Original paper, page 31 · Answer key

1 mark · MCQ
Report issue

Loop-condition comparison count

Control Flow & Loop Tracing

Question 15, GATE 2007: Consider the C fragment:
int j, n;
j = 1;
while (j <= n)
    j = j * 2;
For n>0, how many comparisons are made during execution? The printed choices are below; note the distinction between successful comparisons and the final failing test.
Options:
(A) ceil(log₂ n)+1
(B) n
(C) ceil(log₂ n)
(D) floor(log₂ n)+1
Tap to enlarge
Your answer
Answer & solution

Answer: floor(log2 n)+2 including the final failed test; no exact listed option (no official key was published)

1. Approach. Let k=floor(log₂ n). The successful tests use j=20 through 2k. Then the loop tests 2k+1 once and fails. Check n=1 as a quick boundary example: two tests, one iteration.

2. Derivation and interpretation. Assume n is supplied positive and arithmetic does not overflow. The loop condition is tested with j=1,2,4,...,2^floor(log2 n), all successful, then once more at 2^(floor(log2 n)+1), which fails. Thus body executions and successful comparisons number floor(log2 n)+1, but all comparisons number floor(log2 n)+2. Option D counts successful tests/body executions only. The fragment declares n without assigning it; the question’s n>0 premise must supply its value, and literal fixed-width C may also overflow for large n. No official key is inferred.

Original paper, page 4