Masking bits in VB.NET

Hello !

I'm doing a project wich I need to mask out bits from an integer value.

In VB6 i did this to make it work :

if bSum And 2 ^ 0 Then
 'Bit active
end if

But this way is not working in VB.Net

How can I make it work ?
haakonsAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
It works for me...try this out:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim bSum As Integer
        Dim result As String

        bSum = 3
        If bSum And 2 ^ 0 Then
            result = "IS"
        Else
            result = "IS NOT"
        End If
        MsgBox("bitSum = " & bSum & "    Bit 1 " & result & " active")

        bSum = 2
        If bSum And 2 ^ 0 Then
            result = "IS"
        Else
            result = "IS NOT"
        End If
        MsgBox("bitSum = " & bSum & "    Bit 1 " & result & " active")
    End Sub
0
 
Bob LearnedCommented:
What's wrong in VB.NET?  It works for me.

Bob
0
 
haakonsAuthor Commented:
Can you make it work with option strict ?

Haakon
0
 
haakonsAuthor Commented:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim bSum As Integer
        Dim result As String

        bSum = 3
        If Convert.ToBoolean(bSum And Convert.ToInt32(2 ^ 0)) Then
            result = "IS"
        Else
            result = "IS NOT"
        End If

        MsgBox("bitSum = " & bSum & "    Bit 1 " & result & " active")

        bSum = 2
        If Convert.ToBoolean(bSum And Convert.ToInt32(2 ^ 0)) Then
            result = "IS"
        Else
            result = "IS NOT"
        End If

        MsgBox("bitSum = " & bSum & "    Bit 1 " & result & " active")

    End Sub

I found out how to make it work with option strict. Thank you for your help Bob.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.