How to Perform Bit Manipulation In Visual Basic

ID: Q113476


The information in this article applies to:


SUMMARY

Visual Basic does not explicitly support the Set, Clear, Toggle, and Examine bit manipulation functions. This article describes how to simulate these functions in Visual Basic.


MORE INFORMATION

Bit-level manipulation is common practice for operating systems and programs that need to conserve space. Eight Yes/No, On/Off bits of data may be stored in a single byte rather than using up 8 bytes. Visual Basic can't address the bit level directly, but by using logical operators, it can manipulate bit data by working with one byte at a time.

Example Code to Handle Bit Manipulation

The following example manipulates the Bits of a single Byte of data. An Integer variable is used to store the information after it is retrieved from the Text box because an integer is the smallest addressable numeric type available in Visual Basic. Because an integer is stored in two bytes, you must mask off the high byte to see the results as you manipulate the low-order byte.
  1. Start a new project in Visual Basic. Form1 is created by default.


  2. Add a Command button (Command1), two Text boxes (Text1 and Text2), and five Labels (Label1 ... Label5) to Form1. Stretch each label so that it is wide enough to hold about 40 characters.


  3. Add the following code to the Command1_Click event:
    
       Sub Command1_Click ()
          ' The following clears the high-order byte & returns only the
          ' low order byte:
          Byte% = Text1.Text And &HFF
          Bit% = Text2.Text
          If Bit% <= 7 Then
             Label1.Caption = "The original value of the Byte is " & Byte%
             Temp% = ExamineBit(Byte%, Bit%)
             Enter the following two lines as one, single line of code:
             Label2.Caption =
                "Bit " & Bit% & IIf(Temp%, " is ", " is not ") &   "set"
             Call ClearBit(Byte%, Bit%)
             Enter the following two lines as one, single line of code:
             Label3.Caption =
                "The value with bit " & Bit% & " clear is " & Str$(Byte%)
             Call SetBit(Byte%, Bit%)
             Label4.Caption = "The value with bit " & Bit% & " set is " & Byte%
             Call ToggleBit(Byte%, Bit%)
             Enter the following two lines as one, single line of code:
             Label5.Caption =
                "The value after toggling bit " & Bit% & " is " & Byte%
          Else
             MsgBox ("Please enter a value less than 8 in the Bit TextBox.")
          End If
       End Sub
     


  4. Add the following code to the General Declarations section of Form1:
    
       ' The ClearBit Sub clears the nth bit (Bit%) of an integer (Byte%).
       Sub ClearBit (Byte%, Bit%)
          ' Create a bitmask with the 2 to the nth power bit set:
          Mask% = 2 ^ Bit%
          ' Clear the nth Bit:
          Byte% = Byte% And Not Mask%
       End Sub
    
       ' The ExamineBit function will return True or False depending on
       ' the value of the nth bit (Bit%) of an integer (Byte%).
       Function ExamineBit% (Byte%, Bit%)
          ' Create a bitmask with the 2 to the nth power bit set:
          Mask% = 2 ^ Bit%
          ' Return the truth state of the 2 to the nth power bit:
          ExamineBit% = ((Byte% And Mask%) > 0)
       End Function
    
       ' The SetBit Sub will set the nth bit (Bit%) of an integer (Byte%).
       Sub SetBit (Byte%, Bit%)
          ' Create a bitmask with the 2 to the nth power bit set:
          Mask% = 2 ^ Bit%
          ' Set the nth Bit:
          Byte% = Byte% Or Mask%
       End Sub
    
       ' The ToggleBit Sub will change the state of the nth bit (Bit%)
       ' of an integer (Byte%).
       Sub ToggleBit (Byte%, Bit%)
          ' Create a bitmask with the 2 to the nth power bit set:
          Mask% = 2 ^ Bit%
          ' Toggle the nth Bit:
          Byte% = Byte% Xor Mask%
       End Sub
     


  5. Run the program.


  6. Enter a numeric value in the Text1 box indicating the number you want to manipulate.


  7. Enter a single digit into the Text2 box indicating which bit you want this sample program to change around.

    NOTE: Count bits starting with the least significant bit as the 0 bit. If you enter a value greater than 7 (there are 8 bits, numbered 0 to 7), you will be prompted to enter a number between 0 and 7.


Additional query words: 1.00 2.00 3.00


Keywords          : 
Version           : 
Platform          : 
Issue type        : 

Last Reviewed: June 25, 1999