How do you find the position of the most significant bit (MSB) of an unsigned integer in VB.NET

2 Answers

0 votes
Imports System
Imports System.Numerics

Module Program

    ''' <summary>
    ''' Returns the zero-based position of the most significant set bit (MSB)
    ''' in an unsigned 32-bit integer.
    '''
    ''' Examples:
    '''   1  -> 0
    '''   2  -> 1
    '''   8  -> 3
    '''   15 -> 3
    '''
    ''' Returns:
    '''   -1 if the value is 0 (no bits are set).
    ''' </summary>
    ''' <param name="value">The unsigned integer to examine.</param>
    ''' <returns>
    ''' The zero-based position of the highest set bit, or -1 if value is 0.
    ''' </returns>
    Private Function GetMsbPosition(value As UInteger) As Integer

        ' Zero has no set bits.
        If value = 0UI Then
            Return -1
        End If

        ' BitOperations.LeadingZeroCount returns the number of
        ' zero bits preceding the highest set bit.
		Const BitsPerUInteger As Integer = 32

		Return BitsPerUInteger - 1 - BitOperations.LeadingZeroCount(value)

    End Function

    Sub Main()

        Dim testValues As UInteger() =
        {
            0UI,
            1UI,
            2UI,
            8UI,
            15UI,
            &H1234UI,
            &H80000000UI
        }

        For Each value As UInteger In testValues

            Dim msbPosition As Integer = GetMsbPosition(value)

            If msbPosition >= 0 Then
                Console.WriteLine(
                    $"Value: {value}, MSB position: {msbPosition}")
            Else
                Console.WriteLine(
                    $"Value: {value}, no bits are set")
            End If

        Next

    End Sub

End Module
	
				
					
'
' run:
'
' Value: 0, no bits are set
' Value: 1, MSB position: 0
' Value: 2, MSB position: 1
' Value: 8, MSB position: 3
' Value: 15, MSB position: 3
' Value: 4660, MSB position: 12
' Value: 2147483648, MSB position: 31
'
'
					

 



answered Sep 30 by avibootz
0 votes
Imports System

Module Program

    ''' <summary>
    ''' Returns the zero-based position of the most significant set bit (MSB)
    ''' in an unsigned 32-bit integer.
    '''
    ''' Examples:
    '''   1  -> 0
    '''   2  -> 1
    '''   8  -> 3
    '''   15 -> 3
    '''
    ''' Returns:
    '''   -1 if the value is 0 (no bits are set).
    ''' </summary>
    ''' <param name="value">The unsigned integer to examine.</param>
    ''' <returns>
    ''' The zero-based position of the highest set bit, or -1 if value is 0.
    ''' </returns>
	Private Function GetMsbPosition(value As UInteger) As Integer

		If value = 0UI Then
			Return -1
		End If

		Dim position As Integer = 0

		While value > 1UI
			value >>= 1
			position += 1
		End While

		Return position

	End Function

    Sub Main()

        Dim testValues As UInteger() =
        {
            0UI,
            1UI,
            2UI,
            8UI,
            15UI,
            &H1234UI,
            &H80000000UI
        }

        For Each value As UInteger In testValues

            Dim msbPosition As Integer = GetMsbPosition(value)

            If msbPosition >= 0 Then
                Console.WriteLine(
                    $"Value: {value}, MSB position: {msbPosition}")
            Else
                Console.WriteLine(
                    $"Value: {value}, no bits are set")
            End If

        Next

    End Sub

End Module
					
					
'
' run:
'
' Value: 0, no bits are set
' Value: 1, MSB position: 0
' Value: 2, MSB position: 1
' Value: 8, MSB position: 3
' Value: 15, MSB position: 3
' Value: 4660, MSB position: 12
' Value: 2147483648, MSB position: 31
'
'
					

 



answered Sep 30 by avibootz
...