[.net] Porting VB.net - C# // help needed..

Started by
4 comments, last by DrGUI 18 years, 8 months ago
I want to port this function to C#: ( from Vb.net BigInt )


 Returns the location of the most significant bit
                Public Function MaxBit() As Integer
                    Dim i, j As Integer

                    For i = LastByte To 0 Step -1
                        For j = 7 To 0 Step -1
                            If n(i) And (2 ^ j) Then
                                Return (i * 8) + j
                            End If
                        Next
                    Next
                    ' Must be zero
                    Return 0
                End Function*


I don't understand what this line means: "If n(i) And (2 ^ j) Then" I interpret it this way:

if( n != 0 && ( (int)System.Math.Pow( 2, j ) != 0 ) )
{
}


But I am sure this is not correct.. Thank your for your time, Tanek
Advertisement
I'm pretty sure it would be bitwise AND, so either one of these should work fine:
if(n & 1<
So you think it's:

if( ( n & (int)System.Math.Pow( 2.0d, (double)j ) ) != 0 )


?
That should work, but the following would be even better:

if((n & 1<<j)!=0)
Maybe I'm just being stupid, but what is n? I don't see it defined anywhere.

Here's my code, documented and all [smile]:
/// <summary>/// Returns the highest set bit of <paramref name="value"/>, in the range of 0 to 31, and -1 if/// <paramref name="value"/> equals 0./// </summary>/// <param name="value">The value to find the highest set bit of.</param>/// <returns>/// The highest set bit of <paramref name="value"/>, in the range of 0 to 31, and -1 if/// <paramref name="value"/> equals 0.</returns>public static int HighestBitSet(System.Int32 value){	for (int i = 31; i >= 0; i--)		if ((value & (1 << i)) != 0)			return i;	return -1;}


Hoping that helps...

EDIT: ah yes, you're doing it for a BigInt made from an array of values - I see clearly now!

This topic is closed to new replies.

Advertisement