ProblemYou need to shift, mask, and perform other bitwise manipulations on integers. SolutionVisual Basic 2005 has functions for all the major bit-manipulation techniques, and it's easy to combine these to perform more complicated bitwise calculations as required. DiscussionThere are several operators that are most often thought of as Boolean operators, working with and returning true and False (Boolean) values. However, these operators also accept and return integer values of various sizes, and this is where they can be of value for bit manipulations. These bitwise operators include the following:
The following code demonstrates a sampling of these bit manipulations. You can change the program to experiment with the various operators: Dim result As New System.Text.StringBuilder Dim number As Integer = 7 result.Append(number) result.Append(" <<= 3 … ") number <<= 3 result.AppendLine(number) result.Append(number) result.Append(" Xor 17 … ") number = number Xor 17 result.AppendLine(number) MsgBox(result.ToString()) Figure 6-24 shows the output displayed by this sample code. Figure 6-24. Bit manipulations with Visual Basic 2005See AlsoSearch for "Logical and Bitwise Operators in Visual Basic" in Visual Studio Help to learn more about this topic. |