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.
Set Query
-
- Administrator
- Posts: 78601
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Set Query
There is no built-in function for that, but you can use the And operator:
This uses
15 And 4 = 4
11 And 4 = 0
Code: Select all
Function TestBit(lngNumber As Long, lngBit As Long) As Boolean
TestBit = (lngNumber And 2 ^ lngBit = 2 ^ lngBit)
End Function
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!)
Reason: to correct error (thanks, Stuart!)
Best wishes,
Hans
Hans
-
- Administrator
- Posts: 12627
- Joined: 16 Jan 2010, 15:49
- Location: London, Europe
Re: Set Query
Hans,
An extra "Then" has crept into your code, this needs to be deleted for it to compile.
An extra "Then" has crept into your code, this needs to be deleted for it to compile.
StuartR
-
- Administrator
- Posts: 78601
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Set Query
Thanks, that's what you get when you change your mind while posting.
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 689
- Joined: 27 Jan 2010, 16:45
- Location: Ottawa, Ontario, Canada