<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WinSoft.se &#187; Bitwise operations</title>
	<atom:link href="http://www.winsoft.se/tag/bitwise-operations/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.winsoft.se</link>
	<description>Development with focus on Visual Basic .NET</description>
	<lastBuildDate>Thu, 26 Jan 2012 19:28:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Checking bits in Visual Basic</title>
		<link>http://www.winsoft.se/2010/01/checking-bits-in-visual-basic/</link>
		<comments>http://www.winsoft.se/2010/01/checking-bits-in-visual-basic/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 19:21:06 +0000</pubDate>
		<dc:creator>Anders Hesselbom</dc:creator>
				<category><![CDATA[Microsoft .NET]]></category>
		<category><![CDATA[Bitwise operations]]></category>

		<guid isPermaLink="false">http://www.winsoft.se/?p=892</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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.</p>
<pre>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</pre>
<p>Hold down the right mouse button, and move the mouse around on the form, and see that the text of the form title says &#8220;Yes&#8221;. 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:</p>
<p>Imagine a bit pattern. The value 3 has a pattern that ends with <strong>0011</strong>. Let&#8217;s say that I want to test the last bit in this pattern, then I need a number with the pattern <strong>0001</strong>, 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 <strong>0001</strong>. The bitwise operator <strong>And </strong>returns 1 if we have a hit. So 0011 <strong>And</strong> 0001 equals 0001 (3 <strong>And </strong>1 = 1) and 0011 <strong>And </strong>0010 equals 0010 (3 and 2 = 2) but 0011 <strong>And </strong>0100 equals 0000 (3 <strong>And </strong>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:</p>
<pre>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</pre>
<p><strong>Or </strong>is a bitwise operator that can be used to set bits in a pattern. 0011 <strong>Or </strong>0001 equals 0011 because the last bit in the pattern was already set. 0011 <strong>Or </strong>1000 equals 1011 (3 <strong>Or </strong>8 = 11) because we tell the first bit to become 1.</p>
<p>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.</p>
<pre>Public Enum Flags
	Flag1 = 1
	Flag2 = 2
	Flag3 = 4
	Flag4 = 8
	Flag5 = 16
	Flag6 = 32
	Flag7 = 64
	Flag8 = 128
End Enum</pre>
<p>This function checks what flags has been passed in.</p>
<pre>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</pre>
<p>When called like this, the fourth, fifth and eighth message box will be shown:</p>
<pre>Me.CheckFlags(Flags.Flag4 Or Flags.Flag5 Or Flags.Flag8)</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.winsoft.se/2010/01/checking-bits-in-visual-basic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

