HighestBitSet in C#

I'm trying to optimize a a method that returns the highest bit set in a bit pattern, my current code is :



 private int HighestBitSet(int bitPattern)
{
return
(Convert.ToString(bitPattern, 2).Length - 1)
-
Convert.ToString(bitPattern, 2).IndexOf("1");
}


 


I know that in c++ i can use an assembler intruction to do this but in c# is not possible so, how can i optimize this

Tnks


Answer this question

HighestBitSet in C#

  • gbm1

    I have already found an answer in this site tnks anyway...

  • HighestBitSet in C#