bit addition with control: checked list box  
Author Message
SkiesOfBordom





PostPosted: Visual Basic Language, bit addition with control: checked list box Top

this has been bugging me for awhile.

ok. i have a checked list box with numerous (about 15) flags that need to be set in order for those things will work (or not work) in the game. the flags are arranged in bit order (1,2,4,8,16,etc).

what is supposed to happen:
if index0 is checked, it adds 1 to the bit sum [dmflags], if unchecked, it subtracts 1 from sum.
if index1 is checked, it adds 2 to the bit sum [dmflags], if unchecked, it subtracts 2 from sum.
etc..

what IS happening:
if index0 is checked, it subtracts 1 to the bit sum [dmflags], if unchecked, it subtracts 2 from sum.
if index1 is checked, it subtracts 2 to the bit sum [dmflags], if unchecked, it subtracts 3 from sum.
if any other index is the same (in pairs)

code:

If dmflags_checklist.GetItemCheckState(0) = CheckState.Checked Then

dmflags = dmflags + 1

Else

dmflags = dmflags - 1

End If

If dmflags_checklist.GetItemCheckState(1) = CheckState.Checked Then

dmflags = dmflags + 2

Else

dmflags = dmflags - 2

End If

dmflags_disp.Text = dmflags

any idea of whats going on




Visual Basic17  
 
 
nobugz





PostPosted: Visual Basic Language, bit addition with control: checked list box Top

Do not subtract anything when the checkbox is unchecked. If nothing is checked, you want to end up with 0, not some negative number...



 
 
SkiesOfBordom





PostPosted: Visual Basic Language, bit addition with control: checked list box Top

Do not subtract anything when the checkbox is unchecked. If nothing is checked, you want to end up with 0, not some negative number...

ok, commented out the whole else statement, and now it just adds to the value when checked. it needs to return to 0 after its been unchecked.

i cannot for some reason get it to do that.



 
 
nobugz





PostPosted: Visual Basic Language, bit addition with control: checked list box Top

I might be off completely but this sounds like a classic case of bit-encoding flags. Perhaps it will help if I write it out the dmflags value in binary, then in decimal. Say the game has 8 options:

Nothing checked: dmflags = 00000000 = 0
Option 1 checked: dmflags = 00000001 = 1
Option 2 checked: dmflags = 00000010 = 2
Option 1+2 checked: dmflags = 00000011 = 3
Option 3 checked: dmflags = 00000100 = 4
Option 3+1 checked: dmflags = 00000101 = 5
Option 3+2 checked: dmflags = 00000110 = 6
Option 3+2+1 check: dmflags = 00000111 = 7
Option 4 checked: dmflags = 00001000 = 8
etc.

If dmflags_checklist.GetItemCheckState(0) = CheckState.Checked Then dmflags = dmflags + 1
If dmflags_checklist.GetItemCheckState(1) = CheckState.Checked Then dmflags = dmflags + 2
If dmflags_checklist.GetItemCheckState(2) = CheckState.Checked Then dmflags = dmflags + 4
If dmflags_checklist.GetItemCheckState(3) = CheckState.Checked Then dmflags = dmflags + 8
etc.

The BitArray class might be useful.



 
 
MS Johan Stenberg





PostPosted: Visual Basic Language, bit addition with control: checked list box Top

If you are working with a bit mask, may I suggest that you use the logical operators "Or" and "And" to set/clear bits instead or adding/subtracting...

Dim i As Integer = 0
i = i
Or 2 ' Set bit 1
i = i Or 2 ' Set bit 1 again - effectively a No-op since the bit was already set
i = i
Or 1 ' Set bit 0
i = i
And (Not 1) ' Clear bit 0

Best regards,
Johan Stenberg



 
 
spotty





PostPosted: Visual Basic Language, bit addition with control: checked list box Top

Or if you just wanting to set and unset bits and determine if bits are set.

You provide it with a Byte and determine the bit you want to set allow with whether is a set or unset action and it returns the byte with the appropriate bit set.

Private Function SetUnsetBit(ByVal value As Byte, ByVal bitItem As Integer, ByVal Action As Short) As Byte
        Dim bit As Byte = 2 ^ bitItem
        If Action = 1 Then '//SET
            value = value Or bit
        ElseIf Action = 0 Then  '//UNSET
            value = value And Not bit
        End If
        Return value
    End Function

    Function IsBitSet(ByVal value As Byte, ByVal bitItem As Short) As Boolean
        Dim bit As Byte = 2 ^ bitItem
        If (value And bit) <> 0 Then
            Return True
        Else
            Return False
        End If
    End Function

and an example with a checklistbox with 8 items, button and label

Public Class Form1


    Private Function SetUnsetBit(ByVal value As Byte, ByVal bitItem As Integer, ByVal Action As Short) As Byte
        Dim bit As Byte = 2 ^ bitItem
        If Action = 1 Then '//SET
            value = value Or bit
        ElseIf Action = 0 Then  '//UNSET
            value = value And Not bit
        End If
        Return value
    End Function

    Sub calculateByte()
        Dim i As Integer
        Dim bytevale As Byte = 0

        For i = 0 To Me.CheckedListBox1.Items.Count - 1
            If Me.CheckedListBox1.GetItemCheckState(i) = CheckState.Checked Then
                bytevale = SetUnsetBit(bytevale, i, 1)
            End If
        Next

        Label1.Text = CStr(bytevale)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        calculateByte()
    End Sub
End Class


 
 
SkiesOfBordom





PostPosted: Visual Basic Language, bit addition with control: checked list box Top

this one worked. thx :)