vba check if bit is set in long
'Extremely fast VBA function to test if a specified bit is set
'within a 32-bit Long integer.
'Bits are numbered from 0 to 31, so the first (least significant) bit
'is bit 0.
'Note: we do not inspect bit 31, the sign bit.
Function LongBitIsSet(theLong&, bit As Byte) As Boolean
Static i&, b&()
If (Not Not b) = 0 Then
ReDim b(0 To 30)
For i = 0 To 30
b(i) = 2 ^ i
Next
End If
If bit < 31 Then LongBitIsSet = theLong And b(bit)
End Function
'------------------------------------------------------------------
MsgBox LongBitIsSet(255, 7) '<--displays: True
MsgBox LongBitIsSet(230, 0) '<--displays: False
MsgBox LongBitIsSet(16384, 14) '<--displays: True
MsgBox LongBitIsSet(-16383, 0) '<--displays: True
MsgBox LongBitIsSet(&H7FFFFFFF, 0) '<--displays: True