GATE 2014 (CS02) – C Programming | Q.10

💻 GATE 2014 (CS02) – C Programming | Q.10
Consider the function func shown below:
int func(int num) { int count = 0; while (num) { count++; num >>= 1; } return (count); }
The value returned by func(435) is  

🔍 Let’s trace the function carefully and understand what happens inside the while loop.

</>  C Program
int func(int num) { int count = 0; while (num) { count++; num >>= 1; } return (count); }
💡 What does num >>= 1 mean?

The statement uses the right-shift operator together with the assignment operator.

In simple words, shift the bits of num one position to the right and store the result back in num.

It is equivalent to:
</>  Equivalent Statement
num = num >> 1;
➡️ What happens during the shift?

A right shift by 1 bit moves every bit one position to the right. For a positive integer, this is equivalent to dividing the number by 2, with the remainder discarded.

📌 Key Point: Every time num is right-shifted by 1 bit, its value becomes approximately half of its previous value.
🔢 For func(435)

First, let’s convert 435 into its binary representation.

435 = 256 + 128 + 32 + 16 + 2 + 1 Binary: 110110011
Therefore, 435 is represented as 110110011 in binary.
📌 Important: The binary representation 110110011 contains 9 bits.

This is important because every right shift by 1 bit removes one bit from the right side of the binary number.

How to Convert 435 to Binary

To convert a decimal number into binary, we check the powers of 2 from the largest to the smallest.

The powers of 2 that we need are:

256   128   64   32   16   8   4   2   1

The Simple Rule

At every step, ask:

Is the remaining number greater than or equal to this power of 2?

If YES, write 1 and subtract that power of 2.
If NO, write 0 and do not subtract anything.

Step-by-Step Conversion

1. Check 256:
435 ≥ 256 → 1
435 − 256 = 179

2. Check 128:
179 ≥ 128 → 1
179 − 128 = 51

3. Check 64:
51 < 64 → 0
We cannot subtract 64 because 64 is larger than the remaining number 51.
Remaining number = 51

4. Check 32:
51 ≥ 32 → 1
51 − 32 = 19

5. Check 16:
19 ≥ 16 → 1
19 − 16 = 3

6. Check 8:
3 < 8 → 0
We cannot subtract 8.
Remaining number = 3

7. Check 4:
3 < 4 → 0
We cannot subtract 4.
Remaining number = 3

8. Check 2:
3 ≥ 2 → 1
3 − 2 = 1

9. Check 1:
1 ≥ 1 → 1
1 − 1 = 0

Why does 51 < 64 give 0?

This is the important part to understand.

After using 256 and 128, we have 51 left. The next power of 2 is 64.

51 < 64

We cannot use 64 because 64 is larger than the remaining number 51.

Therefore, we write 0 for 64 and keep the remaining number as 51.

The 0 simply means:

64 is NOT used in representing 435.

Final Result

Power of 2:
256   128   64   32   16   8   4   2   1
Binary digit:
1   1   0   1   1   0   0   1   1

435 = 110110011₂

Remember:
YES → write 1 and subtract.
NO → write 0 and do not subtract.

🔢 Another Method: Repeated Division by 2

A common way to convert a decimal number into binary is to repeatedly divide the number by 2 and record the remainder at each step.

435 ÷ 2 = 217 Remainder = 1 217 ÷ 2 = 108 Remainder = 1 108 ÷ 2 = 54 Remainder = 0 54 ÷ 2 = 27 Remainder = 0 27 ÷ 2 = 13 Remainder = 1 13 ÷ 2 = 6 Remainder = 1 6 ÷ 2 = 3 Remainder = 0 3 ÷ 2 = 1 Remainder = 1 1 ÷ 2 = 0 Remainder = 1
📌 Important Rule: Read the remainders from bottom to top, not from top to bottom.
Therefore, 435 in binary is:
110110011

So, 435 requires 9 binary bits. This will help us understand why the while loop in the given function executes 9 times.

🔍 Now see what happens inside the loop:
Iteration num
Start 435
1 217
2 108
3 54
4 27
5 13
6 6
7 3
8 1
9 0
📌 Observe: The value of num keeps decreasing after every right shift.

Therefore, the loop runs 9 times before num becomes 0.

Therefore:

</>  C
count = 9;

and the function returns:

</>  C
9
✅ Answer: 9
💡 Simple Interpretation

This function counts how many bits are needed to represent a positive integer in binary.

For 435:

435 = 1101100112

The binary representation of 435 contains 9 bits. Therefore, func(435) returns 9.

📌 Key Takeaway: The function repeatedly performs a right shift by 1 bit. Each shift removes one binary digit. Thus, the number of loop iterations is equal to the number of bits required to represent the positive integer in binary.

Exam Shortcut

Whenever you see:

</> C
while(n)
{
count++;
n >>= 1;
}

directly use:

count = floor(log₂(n)) + 1
What does floor mean?
The floor function removes the decimal part and gives the greatest whole number less than or equal to the given number.

For example: floor(3.8) = 3 and floor(5.2) = 5.

For 435:

2⁸ ≤ 435 < 2⁹

Therefore:

floor(log₂(435)) = 8
count = 8 + 1 = 9
Answer = 9 in less than 5 seconds. 🚀

Gopal Krishna

Hey Engineers, welcome to the award-winning blog,Engineers Tutor. I'm Gopal Krishna. a professional engineer & blogger from Andhra Pradesh, India. Notes and Video Materials for Engineering in Electronics, Communications and Computer Science subjects are added. "A blog to support Electronics, Electrical communication and computer students".

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »