1. Home
  2. Computing & Technology
  3. Visual Basic

Bitwise Operations in VB.NET

General Bit Manipulation Code

By Dan Mabbutt, About.com

Nov 25 2007

Although these tricks are fun and educational, they're still no substitute for general bit manipulation. If you really get down to the level of bits, what you want is a way to examine individual bits, set them, or change them. That's the real code that is missing from .NET.

Perhaps the reason it's missing is that it's not that hard to write subroutines that accomplish the same thing.

A typical reason you might want to do this is to maintain what is sometimes called a flag byte. Some applications, especially those written in low level languages like assembler, will maintain eight boolean flags in a single byte. For example, a 6502 processor chip's status register holds this information in a single 8 bit byte:

Bit 7. Negative flag
Bit 6. Overflow flag
Bit 5. Unused
Bit 4. Break flag
Bit 3. Decimal flag
Bit 2. Interrupt-disable flag
Bit 1. Zero flag
Bit 0. Carry flag

(from Wikipedia)

If your code has to work with this kind of data, you need general purpose bit manipulation code. This code will do the job!

' The ClearBit Sub clears the 1 based, nth bit
' (MyBit) of an integer (MyByte).
Sub ClearBit(ByRef MyByte, ByVal MyBit)
   Dim BitMask As Int16
   ' Create a bitmask with the 2 to the nth power bit set:
   BitMask = 2 ^ (MyBit - 1)
   ' Clear the nth Bit:
   MyByte = MyByte And Not BitMask
End Sub

' The ExamineBit function will return True or False
' depending on the value of the 1 based, nth bit (MyBit)
' of an integer (MyByte).
Function ExamineBit(ByVal MyByte, ByVal MyBit) As Boolean
   Dim BitMask As Int16
   BitMask = 2 ^ (MyBit - 1)
   ExamineBit = ((MyByte And BitMask) > 0)
End Function

' The SetBit Sub will set the 1 based, nth bit
' (MyBit) of an integer (MyByte).
Sub SetBit(ByRef MyByte, ByVal MyBit)
   Dim BitMask As Int16
   BitMask = 2 ^ (MyBit - 1)
   MyByte = MyByte Or BitMask
End Sub

' The ToggleBit Sub will change the state
' of the 1 based, nth bit (MyBit)
' of an integer (MyByte).
Sub ToggleBit(ByRef MyByte, ByVal MyBit)
   Dim BitMask As Int16
   BitMask = 2 ^ (MyBit - 1)
   MyByte = MyByte Xor BitMask
End Sub

To demonstrate the code, this routine calls it (parameters not coded on Click Sub):

Private Sub ExBitCode_Click( ...
   Dim Byte1, Byte2 As Byte
   Dim MyByte, MyBit
   Dim StatusOfBit As Boolean
   Dim SelectedRB As String
   StatusLine.Text = ""
   SelectedRB = GetCheckedRadioButton(Me).Name
   Byte1 = ByteNum.Text ' Number to be converted into Bit Flags
   Byte2 = BitNum.Text ' Bit to be toggled
   ' The following clears the high-order byte & returns only the
   ' low order byte:
   MyByte = Byte1 And &HFF
   MyBit = Byte2
   Select Case SelectedRB
      Case "ClearBitButton"
         ClearBit(MyByte, MyBit)
         StatusLine.Text = "New Byte: " & MyByte
      Case "ExamineBitButton"
         StatusOfBit = ExamineBit(MyByte, MyBit)
         StatusLine.Text = "Bit " & MyBit & _
            " is " & StatusOfBit
      Case "SetBitButton"
         SetBit(MyByte, MyBit)
         StatusLine.Text = "New Byte: " & MyByte
      Case "ToggleBitButton"
         ToggleBit(MyByte, MyBit)
         StatusLine.Text = "New Byte: " & MyByte
   End Select
End Sub
Private Function GetCheckedRadioButton( _
   ByVal Parent As Control) _
   As RadioButton
   Dim FormControl As Control
   Dim RB As RadioButton
   For Each FormControl In Parent.Controls
      If FormControl.GetType() Is GetType(RadioButton) Then
         RB = DirectCast(FormControl, RadioButton)
         If RB.Checked Then Return RB
      End If
   Next
   Return Nothing
End Function

The code in action looks like this:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Explore Visual Basic

More from About.com

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2008 About.com, a part of The New York Times Company.

All rights reserved.