xl vba check if bit is set in 64-bit longlong integer code example
Example 1: excel vba second byte from INTEGER
LoByte = n And &HFF '<-- if n is a 2-byte Integer (left byte)
HiByte = (n And &HFF00&) \ &H100 '<-- if n is a 2-byte Integer (right byte)
Example 2: excel vba high 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 3: xl vba is specific bit set in longlong integer
'Extremely fast VBA function to test if a specified bit is set
'within a 64-bit LongLong integer.
'Bits are numbered from 0 to 63, so the first (least significant) bit
'is bit 0.
'Note: we do not inspect bit 63, the sign bit.
'Note: the LongLong data type is only available in 64-bit VBA.
Function LongLongBitIsSet(theLongLong^, bit As Byte) As Boolean
Static i&, b^()
If (Not Not b) = 0 Then
ReDim b(0 To 62)
For i = 0 To 62
b(i) = 2 ^ i
Next
End If
If bit < 63 Then LongLongBitIsSet = theLongLong 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), 0) '<--displays: True
MsgBox LongLongBitIsSet(&H7FFFFFFFFFFFFFff^, 62) '<--displays: True
'
'
'