|
-
Mar 25th, 2008, 09:28 AM
#1
Thread Starter
Hyperactive Member
[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.
-
Mar 25th, 2008, 09:40 AM
#2
Re: [2008] Code is redundant.
where is it looping continuously? have you walked it through the debugger?
-
Mar 25th, 2008, 09:44 AM
#3
Thread Starter
Hyperactive Member
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.
-
Mar 25th, 2008, 09:47 AM
#4
Re: [2008] Code is redundant.
so put a break on that line and single step the debugger from there. is everything what you expect?
-
Mar 25th, 2008, 09:55 AM
#5
Thread Starter
Hyperactive Member
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.
-
Mar 25th, 2008, 09:56 AM
#6
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
-
Mar 25th, 2008, 10:13 AM
#7
Thread Starter
Hyperactive Member
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.
-
Mar 25th, 2008, 10:39 AM
#8
Re: [2008] Code is redundant.
seperate the do function and call it after the cbool(unsigned...
-
Mar 25th, 2008, 11:14 AM
#9
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|