Bit values

bzeus

Member
Joined
Feb 22, 2005
Messages
14
Programming Experience
Beginner
hi,

i have a problem,
- i have a set of bit numbers 2,4,8,16
- the bit value for 6 should be 2,4

how to get this done in vb.net, is there an operator to perform this function?

tnx a lot in advance...
 
You use the bitwise Or operator:
VB.NET:
Dim int As Integer = 2 Or 4
The 'int' variable now contains 6. You normally do this type of thing with an enumeration decorated with the Flags attribute, e.g.
VB.NET:
<Flags()> Public Enum MyEnum
    Value1 = 2
    Value2 = 4
    Value3 = 8
    Value4 = 16
End Enum
You can then use it like this:
VB.NET:
Dim myValue As MyEnum = MyEnum.Value1 Or MyEnum.Value2

MessageBox.Show(myValue.ToString()) 'Displays "Value1, Value2"

If (myValue And MyEnum.Value1) = MyEnum.Value1 Then
    MessageBox.Show("The composite value includes Value1.")
Else
    MessageBox.Show("The composite value does not include Value1.")
End If

If (myValue And MyEnum.Value2) = MyEnum.Value2 Then
    MessageBox.Show("The composite value includes Value2.")
Else
    MessageBox.Show("The composite value does not include Value2.")
End If

If (myValue And MyEnum.Value3) = MyEnum.Value3 Then
    MessageBox.Show("The composite value includes Value3.")
Else
    MessageBox.Show("The composite value does not include Value3.")
End If

If (myValue And MyEnum.Value4) = MyEnum.Value4 Then
    MessageBox.Show("The composite value includes Value4.")
Else
    MessageBox.Show("The composite value does not include Value4.")
End If
 
Back
Top