Jump to content
  • Advertisement
Sign in to follow this  
Tanek

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

This topic is 4850 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
I'm pretty sure it would be bitwise AND, so either one of these should work fine:
if(n & 1<

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!