Set Query

User avatar
Don Wells
5StarLounger
Posts: 689
Joined: 27 Jan 2010, 16:45
Location: Ottawa, Ontario, Canada

Set Query

Post by Don Wells »

I believe that VBA has a built in boolean function to determine if a given number (from the set 2^0; 2^1; 2^2; ... 2^n), is included in the number returned by the sum of all numbers in the set.

i.e.
    4 tested against 15 would return True
    4 tested against 11 would return False

Can anyone point to documentation on the usability of this function?

T.I.A.
Regards
Don

User avatar
HansV
Administrator
Posts: 78601
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Set Query

Post by HansV »

There is no built-in function for that, but you can use the And operator:

Code: Select all

Function TestBit(lngNumber As Long, lngBit As Long) As Boolean
  TestBit = (lngNumber And 2 ^ lngBit = 2 ^ lngBit)
End Function
This uses
15 And 4 = 4
11 And 4 = 0
Last edited by HansV on 14 Dec 2010, 09:03, edited 1 time in total.
Reason: to correct error (thanks, Stuart!)
Best wishes,
Hans

User avatar
StuartR
Administrator
Posts: 12627
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Set Query

Post by StuartR »

Hans,

An extra "Then" has crept into your code, this needs to be deleted for it to compile.
StuartR


User avatar
HansV
Administrator
Posts: 78601
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Set Query

Post by HansV »

Thanks, that's what you get when you change your mind while posting. :blush:
Best wishes,
Hans

User avatar
Don Wells
5StarLounger
Posts: 689
Joined: 27 Jan 2010, 16:45
Location: Ottawa, Ontario, Canada

Re: Set Query

Post by Don Wells »

Thank you Hans and Stuart :thankyou:
Regards
Don