Solved

How to determine the bit value contained a numeric value

Posted on 2003-03-16
Medium Priority
137 Views
Hello,

I'm new to VB so I hope I describe this correctly. My program uses numeric values to determine application settings. I would like to be able to perform a 'bit test' on that numeric value to determine what settings are being used.

Example:
1 - Setting 1
2 - Setting 2
4 - Setting 3
8 - Setting 4
16 - Setting 5
etc...

Value = 11

How would I test the value of 11 to determine which bit settings are used (i.e. 1, 2, 8)?

Thanks,
robgeorge13
0
Question by:robgeorge13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 11

Expert Comment

ID: 8146801
Try this snippet

for n = 0 to 15
if MyByteorLong AND 2^N then

msgbox "bit " & n & " was set"

end if
next
0

LVL 3

Expert Comment

ID: 8147978
Another way of looking at the code above is to AND the value you receive with a single set bit, if the result is > 0 (should only be zero or the value compared) then it is set...

for instance

value = 12  'Bit pattern is 1010

if value AND 8 = 8 then  'bit is set
msgbox "Bit value 8 or the 4th Bit is set"
end if

0

Author Comment

ID: 8158203
Thanks for the information from both of you. It was very helpful. As a result I created a user defined function that I can call throughout the application.

Function fTestBitValue(ByVal iFlag As Long, ByVal iBit As Integer) As Boolean
If (iFlag And 2 ^ iBit) Then
' Matching Bits
fTestBitValue = True
Else
' No match
fTestBitValue = False
End If
End Function
0

LVL 11

Accepted Solution

rdrunner earned 200 total points
ID: 8158389
Function (ByVal iFlag As Long, ByVal iBit As Integer) As Boolean
fTestBitValue (iFlag And 2 ^ iBit)
End Function

This will do... No need for if ;)
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month12 days, 1 hour left to enroll