?
Solved

How to determine the bit value contained a numeric value

Posted on 2003-03-16
4
Medium Priority
?
141 Views
Last Modified: 2010-05-01
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
Comment
Question by:robgeorge13
  • 2
4 Comments
 
LVL 11

Expert Comment

by:rdrunner
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

by:Shaka913
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

by:robgeorge13
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

by:
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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 (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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, 23 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question