Check Bitwise flags

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anil Gupte

    #1

    Check Bitwise flags

    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 Gupte
    Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving

    www.icinema.com


  • Matthias Tacke

    #2
    Re: Check Bitwise flags

    Anil Gupte wrote:
    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.Pa dLeft(5))
    Next
    WriteLine()
    Write("current: ")
    For I = 8 To 1 Step -1
    If MyByte And (2 ^ (I - 1)) Then
    Write((2 ^ (I - 1)).ToString.Pa dLeft(5))
    Else
    Write(" 0")
    End If
    Next
    Write(MyByte.To String.PadLeft( 5))
    ReadLine()
    End Sub

    HTH
    Matthias

    Comment

    • Heikki Leivo

      #3
      Re: Check Bitwise flags


      "Anil Gupte" <anil-list@icinema.co mwrote in message
      news:edzH%23g6Y HHA.4692@TK2MSF TNGP04.phx.gbl. ..
      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(Bi tmask 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.WriteLi ne(CheckBitMask (2, 7).ToString)
      End Sub



      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Check Bitwise flags

        "Anil Gupte" <anil-list@icinema.co mschrieb:
        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
        ///

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

        Comment

        • Anil Gupte

          #5
          Re: Check Bitwise flags

          Thanx, I will check out both solutions (yours and Heikki's).

          Regards,
          --
          Anil Gupte
          Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving

          www.icinema.com

          "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atwrot e in message
          news:O%23DL6o9Y HHA.4872@TK2MSF TNGP03.phx.gbl. ..
          "Anil Gupte" <anil-list@icinema.co mschrieb:
          >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
          ///
          >
          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

          Comment

          • Anil Gupte

            #6
            Re: Check Bitwise flags

            Funny! :-)

            --
            Anil Gupte
            Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving

            www.icinema.com

            "Matthias Tacke" <Matthias@Tacke .dewrote in message
            news:et0gn0$953 $1@news.albasan i.net...
            Anil Gupte wrote:
            >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.Pa dLeft(5))
            Next
            WriteLine()
            Write("current: ")
            For I = 8 To 1 Step -1
            If MyByte And (2 ^ (I - 1)) Then
            Write((2 ^ (I - 1)).ToString.Pa dLeft(5))
            Else
            Write(" 0")
            End If
            Next
            Write(MyByte.To String.PadLeft( 5))
            ReadLine()
            End Sub
            >
            HTH
            Matthias

            Comment

            • Heikki Leivo

              #7
              Re: Check Bitwise flags

              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-



              Comment

              • Mythran

                #8
                Re: Check Bitwise flags



                "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atwrot e in message
                news:O#DL6o9YHH A.4872@TK2MSFTN GP03.phx.gbl...
                "Anil Gupte" <anil-list@icinema.co mschrieb:
                >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
                ///
                >
                --
                M S Herfried K. Wagner
                M V P <URL:http://dotnet.mvps.org/>
                V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
                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


                Comment

                • Heikki Leivo

                  #9
                  Re: Check Bitwise flags

                  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-


                  Comment

                  • Herfried K. Wagner [MVP]

                    #10
                    Re: Check Bitwise flags

                    "Heikki Leivo" <heikkidotleivo @cadworksdotfi. removethisschri eb:
                    >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.
                    '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.

                    --
                    M S Herfried K. Wagner
                    M V P <URL:http://dotnet.mvps.org/>
                    V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

                    Comment

                    Working...