I made the following function that reads a specified number of bits from my internal buffer and returns the value

vb.net Code:
  1. ''' <summary>
  2.         ''' Reads an unsigned value from the buffer.
  3.         ''' </summary>
  4.         ''' <param name="index">The index to start reading.</param>
  5.         ''' <param name="length">The number of bits to read.</param>
  6.         ''' <param name="OutputTo">When this method returns, contains the unsigned value read.</param>
  7.         ''' <returns>An unsigned value read from the buffer.</returns>
  8.         ''' <exception cref="ArgumentException">This method cannot read more than 64 bits.</exception>
  9.         ''' <exception cref="ArgumentException">The value to be read extends past the end of the buffer.</exception>
  10.  
  11.         Public Shared Function unsigned(ByVal index As Int32, ByVal length As Int32, Optional ByRef OutputTo As Long = 0) As Long
  12.             If length > 64 Then
  13.                 Throw New ArgumentException("length cannot be greater than 64")
  14.                 Exit Function
  15.             End If
  16.  
  17.             If index + (length * 8) > OdwinBuffer.Length Then
  18.                 Throw New ArgumentException("end of buffer reached")
  19.                 Exit Function
  20.             End If
  21.  
  22.             Dim num As Integer = 0
  23.             Dim total As Integer = 0
  24.  
  25.             Do
  26.                 If GetBitflagState(index, CInt(Math.Ceiling(length / 8)), num) Then
  27.                     total = total Or (1 << num)
  28.                 End If
  29.                 num += 1
  30.             Loop While num < length
  31.  
  32.             OutputTo = total
  33.             Return OutputTo
  34.         End Function

GetBitflagState is a custom function that determines if a bit is set or not.

I know that big endian just reverses the bytes and it would be easy enough to read it like that but I need to read the bits. I have never worked with big endian before.

any help is great. thanks, troy.