1

I am updating an some old code to be compatible with some newer applications and I came across the following code:

'Display I/O Status
IOState = get_io_status()
For TestBit = 0 To 11
    If 2 ^ TestBit And IOState Then
        T_IOState(TestBit).BackColor = System.Drawing.ColorTranslator.FromOle(&HFF00)
    Else
        T_IOState(TestBit).BackColor = System.Drawing.ColorTranslator.FromOle(&HFF)
    End If
Next TestBit

This is how the previous programmer was storing different booleans (in a short). For the purposes of our new software I would much rather have them stored in an array.

  1. How can I get the booleans out of the short and into an array?

  2. Can someone please explain what is happening in this code with the

    If 2 ^ TestBit And IOState

I dont understand how it works

Community
  • 1
  • 1
jth41
  • 3,808
  • 9
  • 59
  • 109
  • I think this code, which uses extremely efficient bitwise operators, satisfies Brian Kernighan (http://en.wikiquote.org/wiki/Brian_Kernighan) famous saying "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." – Jamie Clayton Jan 04 '14 at 23:47

3 Answers3

2

@D.R. and @Jamie Clayton: 2 ^ TestBit is not a bitwise XOR operation in Visual Basic (there it is just Xor). It is in many C-derived languages like C# you've linked, but in VB this is the power operation, whose result is always a Double value; and that is obviously not good for bit testing. In any case I'd use 1 << TestBit, which does exactly what you want and doesn't suffer useless type conversion.

The code you posted lets me assume the short value is not completely used, just the first 12 bits. So we have a 12 length array. If IOState is an Enum, it provides the HasFlag function.

Dim BoolArray(11) As Boolean ' Yes it's 11, the highest index, not the length.
For TestBit = 0 To 11
    BoolArray(TestBit) = IOState.HasFlag(1 << TestBit)
Next

This will get you the Boolean array. You can have your own HasFlag function with

Function HasFlag(iostate As Short, testFlag As Short) As Boolean
    Return (iostate And testFlag) <> 0
End Function

The And here is bitwise, not Boolean, and if you are not sure what that does, see the links in D.R.'s answer.

Quirin F. Schroll
  • 1,302
  • 1
  • 11
  • 25
1

1) You can test each bit and set the bool int the array accordingly. Testing if a bit is set in a short: Checking if a bit is set or not

2) 2 ^ TestBit is a bitwise XOR operation, e.g.:

2 ... 0010
when TestBit = 3
3 ... 0011

2^TestBit = 0001 -> greater than 0 -> if(2 ^TestBit) would match

So, it checks if different bits are set.

Community
  • 1
  • 1
D.R.
  • 20,268
  • 21
  • 102
  • 205
1

You could try the BitArray which is a simple class that comes with an iterator so that you can For...Each over it.

Dim N As Short = &H13
Dim BA As New BitArray({N})
For Each B In BA
    Console.WriteLine(B)
Next
Chris Haas
  • 53,986
  • 12
  • 141
  • 274