473,320 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
www.keeninc.net
www.icinema.com
Mar 11 '07 #1
9 7727
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.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
Mar 11 '07 #2

"Anil Gupte" <an*******@icinema.comwrote in message
news:ed****************@TK2MSFTNGP04.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(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

Mar 11 '07 #3
"Anil Gupte" <an*******@icinema.comschrieb:
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/>

Mar 11 '07 #4
Thanx, I will check out both solutions (yours and Heikki's).

Regards,
--
Anil Gupte
www.keeninc.net
www.icinema.com

"Herfried K. Wagner [MVP]" <hi***************@gmx.atwrote in message
news:O%****************@TK2MSFTNGP03.phx.gbl...
"Anil Gupte" <an*******@icinema.comschrieb:
>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/>

Mar 11 '07 #5
Funny! :-)

--
Anil Gupte
www.keeninc.net
www.icinema.com

"Matthias Tacke" <Ma******@Tacke.dewrote in message
news:et**********@news.albasani.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.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

Mar 11 '07 #6
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-

Mar 11 '07 #7


"Herfried K. Wagner [MVP]" <hi***************@gmx.atwrote in message
news:O#**************@TK2MSFTNGP03.phx.gbl...
"Anil Gupte" <an*******@icinema.comschrieb:
>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
Mar 12 '07 #8
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-
Mar 12 '07 #9
"Heikki Leivo" <he************@cadworksdotfi.removethisschrieb:
>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/>

Mar 12 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Michael B. Trausch | last post by:
I have a question regarding bitwise operators, I've been trying to figure this out for about two days now, and I just can't seem to get it. What I'm trying to do is use a variable to hold a bitmask...
9
by: Johnathan Doe | last post by:
Hi, I am having problems understanding bitwise idioms that I see frequently in source code. I understand that they work at the individual bit level and that 0 | 1 = 1, 1 & 1 = 1 and so on...
8
by: Paul E Collins | last post by:
Suppose I have a few Keys objects: Keys k1 = Keys.V; // V Keys k2 = Keys.Control | Keys.V; // Ctrl+V Keys k3 = Keys.Shift | Keys.J; // Shift+J I need to determine which of these include the...
9
by: Christopher Weaver | last post by:
I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges...
37
by: James Radke | last post by:
Hello, I found some code that I could use in my application on the web, but it is written in C#, and I would like to convert it to VB. And I am having problems with one section. Is there...
17
by: zirconx | last post by:
I'm trying to understand how the bitwise AND can be used. I've read about what it does but am having trouble applying it practically. I'm working on a system that somebody else wrote and they...
29
by: Carl Banks | last post by:
Anyone with me here? (I know the deadline for P3 PEPs has passed; this is just talk.) Not many people are bit-fiddling these days. One of the main uses of bit fields is flags, but that's not...
10
by: Rob Wilkerson | last post by:
I'm attempting to do some work around existing code that uses bitwise operations to manage flags passed into a function and I'm quite frankly unequipped to do so. I've never done much with bitwise...
9
by: Ioannis Vranos | last post by:
Well when we have an expression like this: int obj= first | second; Why is this style preferred than the equivalent: int obj= first+ second; ?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.