Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binary Testing 2

Status
Not open for further replies.

oharab

Programmer
May 21, 2002
2,152
GB
As part of another process I have 3 options which I have assigned a value to:
Appraisee=1
Line1=2
Line2=4

If I am given an integer (0-7) how do I check which options have been selected, eg X=6 means line1 and line2 are selected. X=5 means Appraisee and Line2, etc. I'm sure there's an easy way to do this, but buggered if I can remember how. Any ideas?

Ben

----------------------------------------------
Ben O'Hara "Where are all the stupid people from...
...And how'd they get so dumb?"
rockband.gif
NoFX-The Decline
----------------------------------------------
 
Where n is a bit number between 0 and whatever.

BitSet = ((2^n) and (overallvalue) = 2^n)

mmilan
 
Erm don't get what you mean?
What I want is something like
Code:
X=optionsvalue
if x means appraisee has been chosen (ie 1,3,5 or 7) then
  do something
end if

if x means line1 has been chosen (ie 2,3,6 or 7) then
  do something
end if

if x means line2 has been chosen (ie 4,5,6 or 7) then
  do something
end if

I know I could use the values in an if statement, but I'm sure there's a better way, and I think mmilan is on the right tracks for me.

Cheers

Ben

----------------------------------------------
Ben O'Hara "Where are all the stupid people from...
...And how'd they get so dumb?"
rockband.gif
NoFX-The Decline
----------------------------------------------
 
Ben,

Try this:

Assume all variables have been dimensioned already
Code:
Appraisee = 1 
Line1 = 2
Line2 = 4

x = optionsvalue
If x And Appraisee Then
  ' Appraisee active
  do something
End If

If x And Line1 Then
  ' line1 active
  do something
End If

If x And Line2 Then
  ' line2 active
  do something
End If

The And operator in VB actualy does an Bit wise comparison so this will work.

-Sean
 
It will indeed work - it's similar to my own solution, except you are using constants for particular cases...

To wrap this up nicely then, where lValue is the value you are testing, and iBit is the number of the bit you wish to test, what you're looking for is something like this...

public function BitIsSet(lValue as long, iBit as integer) as boolean

BitIsSet = ((lvalue and 2^IBit) > 0)

end function

Here is a table of the results of the function
lvalue ibit Result
20 0 False
20 1 False
20 2 True
20 3 False
20 4 True

20 in Binary is 10100. Now can you see what I'm doing?

mmilan

 
Thanks guys for your help with this, I ended up using Sean's method, but mmilan's extra explanation has helped explain what's going on.

Cheers

Ben

----------------------------------------------
Ben O'Hara "Where are all the stupid people from...
...And how'd they get so dumb?"
rockband.gif
NoFX-The Decline
----------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top