vba check if bit set in integer code example
Example 1: excel vba check if bit is set in integer
Function IntegerBitIsSet(theInteger%, bit As Byte) As Boolean
Static i&, b%()
If (Not Not b) = 0 Then
ReDim b(0 To 14)
For i = 0 To 14
b(i) = 2 ^ i
Next
End If
If bit < 15 Then IntegerBitIsSet = theInteger And b(bit)
End Function
MsgBox IntegerBitIsSet(255, 7)
MsgBox IntegerBitIsSet(230, 0)
MsgBox IntegerBitIsSet(16384, 14)
MsgBox IntegerBitIsSet(-16383, 0)
Example 2: excel vba set bit in byte
Function ByteBitIsSet(theByte As Byte, bit As Byte) As Boolean
Static i&, b() As Byte
If (Not Not b) = 0 Then
ReDim b(0 To 7)
For i = 0 To 7
b(i) = 2 ^ i
Next
End If
If bit < 8 Then ByteBitIsSet = theByte And b(bit)
End Function
MsgBox ByteBitIsSet(255, 7)
MsgBox ByteBitIsSet(230, 0)