I have not posted here nor have I worked with this code for a while.

I have a function that can read a value from a byte buffer in bits. I choose bits so that nybbles may be read. I have another function that determines if a certain bit is set or not. I use the two functions to read the value from the buffer. Here are the functions:

Code:
Public Shared Function Unsigned(ByVal Index As Integer, ByVal Length As Integer) As Long

            ' Check if the buffer is empty
            If Buffer.Length <= 0 Then
                Throw New InvalidOperationException("The buffer is empty. Load a file into it using OpenFile.")
                Exit Function
            End If

            ' Check the length to read to make sure it is valid
            If Length > 64 OrElse Length < 1 Then
                Throw New ArgumentException("Length cannot be greater than 64 or less than 1.")
                Exit Function
            End If

            ' Check if reading will extend past the end of the buffer
            If Index + (Length * 8) > Buffer.Length Then
                Throw New ArgumentException("End of buffer reached.")
                Exit Function
            End If

            ' The bit currently being checked
            Dim Bit As Integer = 0
            Dim Total As Long = 0

            Do
                ' Check each bit to see if it is set
                If BitflagState(Index, Length, Bit) Then
                    Total = Total Or (1 << Bit)
                End If
                Bit += 1
            Loop Until Bit = Length

            Return Total
        End Function
Code:
Public Shared Function BitflagState(ByVal Index As Int32, ByVal Length As Int32, ByVal Bit As Integer) As Boolean
            Try
                Return CBool(Unsigned(Index, Length) And (1 << Bit))
            Catch ex As Exception
                ShowErrorDialog(ex.StackTrace, ex.Message)
            End Try
        End Function
Now, the code just loops continuously until I get a stack overflow exception. The obvious reason is that the functions are not compatible. I was wondering how I could accomplish this. I want to be able to read values from a byte buffer but also read them bit by bit. Any ideas?