Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.
2010-01-19
There are several times that you need to master bitwise operations in Visual Basic. Imagine the following code in a form. Whenever you move the mouse, it checks if the right mouse button is held down.
Private Sub M(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles Me.MouseDown, Me.MouseMove, Me.MouseUp If e.Button = Windows.Forms.MouseButtons.Right Then Me.Text = "Yes" Else Me.Text = "No" End If End Sub
Hold down the right mouse button, and move the mouse around on the form, and see that the text of the form title says “Yes”. But this code fails when you hold down the right button at the same time as another button. Checking the state of the mouse buttons is one situation you need to know bitwise operations. Checking file attributes is another. Also, you might want to develop a function that takes one or more options in one argument. This is the basics:
Imagine a bit pattern. The value 3 has a pattern that ends with 0011. Let’s say that I want to test the last bit in this pattern, then I need a number with the pattern 0001, that is, a bit pattern with zeros on all position except for the position I want to test. The value 1 has a pattern that ends with 0001. The bitwise operator And returns 1 if we have a hit. So 0011 And 0001 equals 0001 (3 And 1 = 1) and 0011 And 0010 equals 0010 (3 and 2 = 2) but 0011 And 0100 equals 0000 (3 And 4 = 0) because the one in 0100 does not hit a one in 0011. So the bug free version of my program could look like this:
Private Sub M(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles Me.MouseDown, Me.MouseMove, Me.MouseUp If (e.Button And Windows.Forms.MouseButtons.Right) = _ Windows.Forms.MouseButtons.Right Then Me.Text = "Yes" Else Me.Text = "No" End If End Sub
Or is a bitwise operator that can be used to set bits in a pattern. 0011 Or 0001 equals 0011 because the last bit in the pattern was already set. 0011 Or 1000 equals 1011 (3 Or 8 = 11) because we tell the first bit to become 1.
For this example (also in a form), I have created an enumeration with 8 flags. All values has a bit pattern with one 1 and 0 at all other positions.
Public Enum Flags Flag1 = 1 Flag2 = 2 Flag3 = 4 Flag4 = 8 Flag5 = 16 Flag6 = 32 Flag7 = 64 Flag8 = 128 End Enum
This function checks what flags has been passed in.
Private Sub CheckFlags(ByVal Arg As Flags) If (Arg And Flags.Flag1) = Flags.Flag1 Then MessageBox.Show("Flag 1 is set") End If If (Arg And Flags.Flag2) = Flags.Flag2 Then MessageBox.Show("Flag 2 is set") End If If (Arg And Flags.Flag3) = Flags.Flag3 Then MessageBox.Show("Flag 3 is set") End If If (Arg And Flags.Flag4) = Flags.Flag4 Then MessageBox.Show("Flag 4 is set") End If If (Arg And Flags.Flag5) = Flags.Flag5 Then MessageBox.Show("Flag 5 is set") End If If (Arg And Flags.Flag6) = Flags.Flag6 Then MessageBox.Show("Flag 6 is set") End If If (Arg And Flags.Flag7) = Flags.Flag7 Then MessageBox.Show("Flag 7 is set") End If If (Arg And Flags.Flag8) = Flags.Flag8 Then MessageBox.Show("Flag 8 is set") End If End Sub
When called like this, the fourth, fifth and eighth message box will be shown:
Me.CheckFlags(Flags.Flag4 Or Flags.Flag5 Or Flags.Flag8)
Categories: Microsoft .NET
Tags: Bitwise operations
Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!
Leave a Reply