Results 1 to 9 of 9

Thread: [2008] Code is redundant.

  1. #1

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    [2008] Code is redundant.

    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?
    Prefix has no suffix, but suffix has a prefix.

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2008] Code is redundant.

    where is it looping continuously? have you walked it through the debugger?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: [2008] Code is redundant.

    I apologize for that. When the debugger hits this line:

    Code:
    Return CBool(Unsigned(Index, Length) And (1 << Bit))
    It jumps back to the start of the Unsigned function and loops from there.
    Prefix has no suffix, but suffix has a prefix.

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2008] Code is redundant.

    so put a break on that line and single step the debugger from there. is everything what you expect?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: [2008] Code is redundant.

    I confused you.

    I start with the function Unsigned. It reads all the way to "If BitflagState(Index, Length, Bit) Then" and it jumps to the function BitflagState. When it reaches the line "Return CBool(Unsigned(Index, Length) And (1 << Bit))" in function BitflagState, it jumps back to the function Unsigned.

    This is the way it was written but not the way I expected it to work. Weird, eh? The loop continues until the error occurs.

    What I was looking for was a way to make the function Unsigned work correctly. I want it to read from a byte array one bit at a time.

    So, if I set the Length property to 8 then it will read 8 bits from the array. I could have sworn this worked last time I used it but it doesn't now.

    Any ideas on how I could fix this?
    Prefix has no suffix, but suffix has a prefix.

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2008] Code is redundant.

    It's not that they aren't compatible.... you've got a circular reference... Unsigned calls BitState, which calls Unsigned, which calls bitState, which calls.... you see where this is going? It's a box inside a box inside a box inside a box inside a box inside a box inside a box inside a box inside a box inside a box inside a box inside a box inside a box inside a box inside a box inside a box inside a box inside a box inside a box inside a box inside a box inside a box inside a box inside a box inside a box BOOOM!

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: [2008] Code is redundant.

    @techgnome: I understand that. I just do not understand how to get it to work right. Or rather, how to fix it.
    Prefix has no suffix, but suffix has a prefix.

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2008] Code is redundant.

    seperate the do function and call it after the cbool(unsigned...
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: [2008] Code is redundant.

    Ok. I figured out a solution. I changed the Do...Loop to a For...Next like so:

    Code:
    For Bit As Integer = 0 To Length - 1
        ' Check each bit to see if it is set
        If CBool(Buffer(Index) And (1 << Bit)) Then
            Total = Total Or (1 << Bit)
        End If
        If Bit Mod 8 = 0 AndAlso bit > 0 Then
            Index = Index + 1
        End If
    Next Bit
    The second part of that code allows me to read the next byte if the Length property allows it. Please let me know if there is a simpler way of doing this. Thanks.
    Prefix has no suffix, but suffix has a prefix.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width