Check Bitwise flags

  • Thread starter Thread starter Anil Gupte
  • Start date Start date
A

Anil Gupte

This is somethings I used to do often in other languages (such as Lisp), but
don't know how to do in VB.

I want to check if a certain bit (flag) is set. For example if I want to
check whether the 2nd bit is set, I compare it to binary 2

7 and 2 should return true (111 AND 010 = true)

How does one do this in VB?

Thanx,
 
Anil said:
This is somethings I used to do often in other languages (such as Lisp), but
don't know how to do in VB.

I want to check if a certain bit (flag) is set. For example if I want to
check whether the 2nd bit is set, I compare it to binary 2

7 and 2 should return true (111 AND 010 = true)

How does one do this in VB?

The same way,

Sub Main()
Dim MyByte As Byte = &HAA
Dim I As Integer
WriteLine("Bit_no :____8____7____6____5____4____3____2____1")
Write("Bitval :")
For I = 8 To 1 Step -1
Write((2 ^ (I - 1)).ToString.PadLeft(5))
Next
WriteLine()
Write("current:")
For I = 8 To 1 Step -1
If MyByte And (2 ^ (I - 1)) Then
Write((2 ^ (I - 1)).ToString.PadLeft(5))
Else
Write(" 0")
End If
Next
Write(MyByte.ToString.PadLeft(5))
ReadLine()
End Sub

HTH
Matthias
 
Anil Gupte said:
This is somethings I used to do often in other languages (such as Lisp),
but don't know how to do in VB.

I want to check if a certain bit (flag) is set. For example if I want to
check whether the 2nd bit is set, I compare it to binary 2

7 and 2 should return true (111 AND 010 = true)

How does one do this in VB?

You already gave the answer by yourself. I don't quite see why you need a
function to do this, but who cares. So:


Function CheckBitMask(Bitmask As Long, Value As Long) As Boolean
Return CBool(Bitmask And Value)
End Function

Sub Foo()
'> 7 and 2 should return true (111 AND 010 = true)
Console.WriteLine(CheckBitMask(2, 7).ToString)
End Sub
 
Anil Gupte said:
This is somethings I used to do often in other languages (such as Lisp),
but don't know how to do in VB.

I want to check if a certain bit (flag) is set. For example if I want to
check whether the 2nd bit is set, I compare it to binary 2

7 and 2 should return true (111 AND 010 = true)

\\\
If CBool(Foo And &H2) Then
...
End If
///
 
Thanx, I will check out both solutions (yours and Heikki's).

Of course Herfrieds solution is the way to go, since there is no need to
wrap a logical operation such as foo And bar into a function. I realized
later that you didn't even ask for a function, just a solution. Sometimes a
solution is far too obvious to be seen directly.

-h-
 
Herfried K. Wagner said:
\\\
If CBool(Foo And &H2) Then
...
End If
///

Should it not be:

If Foo And &H2 = &H2
...
End If

?? Performing a bitwise AND will give a result of &H2 if &H2 is set to ON
and will give you 0 if &H2 is not set... Also, doing it this way, you don't
need to convert to boolean since adding "= &H2" makes it a boolean
expression...

HTH,
Mythran
 
Should it not be:
If Foo And &H2 = &H2
?? Performing a bitwise AND will give a result of &H2 if &H2 is set to ON
and will give you 0 if &H2 is not set... Also, doing it this way, you
don't need to convert to boolean since adding "= &H2" makes it a boolean
expression...

What else could Foo And &H2 be than 0 and &h2? I don't quite see your point,
there is only one bit set in &H2. In languages such as c/c++ there is no
need to cast something to boolean, since any nonzero value is defined to be
"true".

-h-
 
Heikki Leivo said:
What else could Foo And &H2 be than 0 and &h2? I don't quite see your
point, there is only one bit set in &H2.

'CBool' will return 'True' if a bit in the value passed to it is set to 1.
In languages such as c/c++ there is no need to cast something to boolean,
since any nonzero value is defined to be "true".

Such automatic conversions are performed with 'Option Strict Off', but I
think that the more explicit way is the better choice
most times.
 
Back
Top