-1

Scenario:

Let's say for example, I have two functions.
First function is concerned with reading a single bit, thus returing true or false.
Second function is concerned with reading a variable number of bits using first functions n times where n is the number if bits.

First Function:

private bool ReadBit ( )
{
    .
    .
    .
}

Second Function: ( Recursive is using here instead of loop iteration well-known techniques )

public List<bool> ReadBits ( int Value ) //Value = Number of Bits
{
    List<bool> Result = new List<bool> ( );

    if ( Value == 0 )
    {
        return Result;
    }
    else
    {
        Result . Add ( ReadBit ( ) );

        return ReadBits ( --Value );   //OPTION 1
        ReadBits ( --Value );   //OPTION 2

    }
}

I Know OPTION 2 will throw an error about "not all code paths return a value".
This is not the problem as I can deceive the compiler many ways !

My Question:

What are the real deference between OPTION 1 & OPTION 2 ?
I swear both will do the recursive concept if we add a return line somewhere with OPTION 2 & re-order lines somewhat.

5
  • There are several issues here; first, you would need to do Result.AddRange(ReadBit(Value)). Second, instead of decrementing Value, you need to divide it by 2: bits are powers of two; not additions of one. Thus, Option 1 is what you need to do; I'm not sure where you're going with Option 2 or why you would want to "deceive" the compiler...
    – eouw0o83hf
    Mar 8, 2012 at 20:09
  • How exactly do you think you can deceive the compiler here? I think it's just OPTION 1 or OPTION 3: iterate. Mar 8, 2012 at 20:10
  • @eouw0o83hf First, I need it the WRONG way :) , I'm just solving the Big/Little Endian technique this way :) Mar 8, 2012 at 20:18
  • @eouw0o83hf Second, No this is the number of bits not the bits themselves !!! why deviding it by 2 ! Mar 8, 2012 at 20:20
  • @HenkHolterman No. It can be deceived by adding an unreachable return null; line. Mar 8, 2012 at 20:32

2 Answers 2

6

The difference is this:

  • In option 1 you return the wrong result.
  • In option 2 you return no result (and this is invalid, hence the compiler error).

Of course, you want neither. You want to return the correct result:

Result.Add(ReadBit());
Result.AddRange(ReadBits(value - 1));
return Result;

That is, you first add the currently read bit, and then append all the next read bits (that were created recursively).

However, this is wildly inefficient. It’s much more efficient to reverse the process, thus creating only a single list.

public List<bool> ReadBits(int Value)
{
    if (Value == 0)
    {
        List<bool> Result = new List<bool>();
    }
    else
    {
        List<bool> Result = ReadBits(value - 1);
        Result.Add(ReadBit());
    }
    return Result;
}

But note that this will of course reverse the order of your result list.

Much as I like recursion, why not go with the iterative approach here that can be expressed much more naturally in C#?

3
  • First, I need it the WRONG way :) , I'm just solving the Big/Little Endian technique this way :) Mar 8, 2012 at 20:15
  • Second, It's really a brilliant method you used to recursive :D .But I can reverse the order of bits again by swamping the ReadBits and ReadBit lines, true ? Mar 8, 2012 at 20:29
  • 1
    @Ahmed How would you do that? You cannot swap the lines because the second line uses the declaration from the first. Mar 8, 2012 at 20:33
1

Why not just go with a much more straightforward approach, like:

public List<bool> ReadBits(int value)
{
 List<bool> result = new List<bool>();
 while(value > 0)
 {
  result.Add(value & 1 == 1);
  value /= 2;
 }
 return result;
}

Edit: My solution above assumes you wanted an actual binary representation of the object as a List of bools.

2
  • I was just wondering the difference, It's a just made code for the question. Please refer to the Second Function line ( Recursive is using here instead of loop iteration well-known techniques ) Mar 8, 2012 at 20:10
  • 1
    This code doesn’t really do anything and it will loop indefinitely unless value == 0 at the beginning. Mar 8, 2012 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.