excelvba is specific bit set in long integer code example
Example 1: vba low word from Long Integer
'If n is a 4-byte Long Integer, the Low (Left) Word is:
If n And &H8000& Then
LoWord = n Or &HFFFF0000
Else
LoWord = n And &HFFFF&
End If
'If n is a 4-byte Long Integer, the High (Right) Word is:
HiWord = (n And &HFFFF0000) \ &H10000
Example 2: excel vba long to bits
Function LongToBits$(ByVal n&)
Dim i&
LongToBits = "00000000000000000000000000000000"
If n And &H80000000 Then
Mid$(LongToBits, 1, 1) = "1"
n = n And Not &H80000000
End If
For i = 32 To 2 Step -1
If n And 1 Then Mid$(LongToBits, i, 1) = "1"
n = n \ 2
Next
End Function
'------------------------------------------------------------------------------
MsgBox ByteToBits(0) '<--displays: 00000000000000000000000000000000
MsgBox LongToBits(293781237) '<--displays: 00010001100000101011111011110101
MsgBox ByteToBits(-1) '<--displays: 11111111111111111111111111111111
Example 3: excel vba is bit set 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.
'Note: any number of bits can be specified in one call.
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