private int MostSignificantBitSet( int val ) { int result = 0; while ( val != 0 ) { result++; val >>= 1; } return result - 1; }