vba is bit set in integer

'VBA function to create a bitmask in a Long Integer:

Function BitMask&(ParamArray a())
    Dim i&, n&, m
    For i = 0 To UBound(a)
        If i > 31 Then Exit Function
        m = a(i)
        If m <> 0 Then
            If Len(m) Then
                BitMask = BitMask Or Abs(m) * 2 ^ i
            End If
        End If
    Next
End Function
  
'-----------------------------------------------------------------------------
  
Dim mask&
  
mask = BitMask(1, 0, 0, 0, 1)
MsgBox mask   '<--displays:  17         (00000000000000000000000000010001)

mask = BitMask(False, True, True)
MsgBox mask   '<--displays:  6          (00000000000000000000000000000110)

    
'-----------------------------------------------------------------------------
  
'The above function is fast. But for the fastest possible native code, 
'exponentiation (slow) can be optimized, by a lookup table. The following
'function uses the ShiftLeft() function, which is highly optimized. It is
'literally a hundred times faster:
  
Function BitMask&(ParamArray a())
    Dim i&, n&, m
    For i = 0 To UBound(a)
        If i > 31 Then Exit Function
        m = a(i)
        If m <> 0 Then
            If Len(m) Then
                BitMask = BitMask Or ShiftLeft(Abs(m), i)
            End If
        End If
    Next
End Function

Function ShiftLeft&(ByVal n&, Optional ByVal shifts& = 1)
    Dim d&
    Select Case shifts
        Case 1:  d = 2& * (n And 1073741823): If n And 1073741824 Then d = d Or &H80000000
        Case 2:  d = 4& * (n And 536870911):  If n And 536870912 Then d = d Or &H80000000
        Case 3:  d = 8& * (n And 268435455):  If n And 268435456 Then d = d Or &H80000000
        Case 4:  d = 16& * (n And 134217727): If n And 134217728 Then d = d Or &H80000000
        Case 5:  d = 32& * (n And 67108863):  If n And 67108864 Then d = d Or &H80000000
        Case 6:  d = 64& * (n And 33554431):  If n And 33554432 Then d = d Or &H80000000
        Case 7:  d = 128& * (n And 16777215): If n And 16777216 Then d = d Or &H80000000
        Case 8:  d = 256& * (n And 8388607):  If n And 8388608 Then d = d Or &H80000000
        Case 9:  d = 512& * (n And 4194303):  If n And 4194304 Then d = d Or &H80000000
        Case 10: d = 1024& * (n And 2097151): If n And 2097152 Then d = d Or &H80000000
        Case 11: d = 2048& * (n And 1048575): If n And 1048576 Then d = d Or &H80000000
        Case 12: d = 4096& * (n And 524287):  If n And 524288 Then d = d Or &H80000000
        Case 13: d = 8192& * (n And 262143):  If n And 262144 Then d = d Or &H80000000
        Case 14: d = 16384& * (n And 131071): If n And 131072 Then d = d Or &H80000000
        Case 15: d = 32768 * (n And 65535):   If n And 65536 Then d = d Or &H80000000
        Case 16: d = 65536 * (n And 32767&):  If n And 32768 Then d = d Or &H80000000
        Case 17: d = 131072 * (n And 16383&): If n And 16384& Then d = d Or &H80000000
        Case 18: d = 262144 * (n And 8191&):  If n And 8192& Then d = d Or &H80000000
        Case 19: d = 524288 * (n And 4095&):  If n And 4096& Then d = d Or &H80000000
        Case 20: d = 1048576 * (n And 2047&): If n And 2048& Then d = d Or &H80000000
        Case 21: d = 2097152 * (n And 1023&): If n And 1024& Then d = d Or &H80000000
        Case 22: d = 4194304 * (n And 511&):  If n And 512& Then d = d Or &H80000000
        Case 23: d = 8388608 * (n And 255&):  If n And 256& Then d = d Or &H80000000
        Case 24: d = 16777216 * (n And 127&): If n And 128& Then d = d Or &H80000000
        Case 25: d = 33554432 * (n And 63&):  If n And 64& Then d = d Or &H80000000
        Case 26: d = 67108864 * (n And 31&):  If n And 32& Then d = d Or &H80000000
        Case 27: d = 134217728 * (n And 15&): If n And 16& Then d = d Or &H80000000
        Case 28: d = 268435456 * (n And 7&):  If n And 8& Then d = d Or &H80000000
        Case 29: d = 536870912 * (n And 3&):  If n And 4& Then d = d Or &H80000000
        Case 30: d = 1073741824 * (n And 1&): If n And 2& Then d = d Or &H80000000
        Case 31: If n And &H1& Then d = &H80000000 Else d = &H0&
        Case 0:  d = n
    End Select
    ShiftLeft = d
End Function
  

3.33
9
WillS 110 points

                                    'Extremely fast VBA function to test if a specified bit is set 
'within a 16-bit Integer.

'Bits are numbered from 0 to 15, so the first (least significant) bit
'is bit 0.

'Note: we do not inspect bit 15, the sign bit.


Function IntegerBitIsSet(theInteger%, bit As Byte) As Boolean
    Static i&amp;, 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 &lt; 15 Then IntegerBitIsSet = theInteger And b(bit)
End Function

'------------------------------------------------------------------
MsgBox IntegerBitIsSet(255, 7)      '&lt;--displays: True
MsgBox IntegerBitIsSet(230, 0)      '&lt;--displays: False
MsgBox IntegerBitIsSet(16384, 14)   '&lt;--displays: True
MsgBox IntegerBitIsSet(-16383, 0)   '&lt;--displays: True




3.33 (9 Votes)
0
3.25
4
Jinha87 120 points

                                    'Extremely fast VBA function to test if a specified bit is set 
'within a Byte.

'Bits are numbered from 0 to 7, so the first (least significant) bit
'is bit 0.

Function ByteBitIsSet(theByte As Byte, bit As Byte) As Boolean
    Static i&amp;, 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 &lt; 8 Then ByteBitIsSet = theByte And b(bit)
End Function

'------------------------------------------------------------------
MsgBox ByteBitIsSet(255, 7)      '&lt;--displays: True
MsgBox ByteBitIsSet(230, 0)      '&lt;--displays: False



3.25 (4 Votes)
0
3.6
5
Samwise 75 points

                                    'Extremely fast VBA function to convert a binary string to a Byte:

Function BitsToByte(bits$) As Byte
    Dim i&amp;
    Static b() As Byte
    If LenB(bits) &gt; 16 Then Exit Function
    If LenB(bits) = 16 Then
        b = bits
    Else
        b = String$(8 - Len(bits), &quot;0&quot;) &amp; bits
    End If
    For i = 0 To 14 Step 2
        BitsToByte = 2 * BitsToByte Or (b(i) Xor 48)
    Next
End Function


'Example:

MsgBox BitsToByte(&quot;00001100&quot;)		'&lt;--displays: 12
MsgBox BitsToByte(&quot;10000001&quot;)		'&lt;--displays: 129
'
'
'

3.6 (5 Votes)
0
4
7
L.w 130 points

                                    Public Function ByteToBits$(ByVal n&amp;)
    ByteToBits = &quot;00000000&quot;
    If n And 1 Then MidB$(ByteToBits, 15) = &quot;1&quot;
    If n And 2 Then MidB$(ByteToBits, 13) = &quot;1&quot;
    If n And 4 Then MidB$(ByteToBits, 11) = &quot;1&quot;
    If n And 8 Then MidB$(ByteToBits, 9) = &quot;1&quot;
    If n And 16 Then MidB$(ByteToBits, 7) = &quot;1&quot;
    If n And 32 Then MidB$(ByteToBits, 5) = &quot;1&quot;
    If n And 64 Then MidB$(ByteToBits, 3) = &quot;1&quot;
    If n And 128 Then MidB$(ByteToBits, 1) = &quot;1&quot;
End Function

'------------------------------------------------------------------------------

MsgBox ByteToBits(0)		'&lt;--displays: 00000000
MsgBox ByteToBits(170)		'&lt;--displays: 10101010
MsgBox ByteToBits(255)		'&lt;--displays: 11111111

4 (7 Votes)
0
4
9

                                    Function LongToBits$(ByVal n&amp;)
    Dim i&amp;
    LongToBits = &quot;00000000000000000000000000000000&quot;
    If n And &amp;H80000000 Then
        Mid$(LongToBits, 1, 1) = &quot;1&quot;
        n = n And Not &amp;H80000000
    End If
    For i = 32 To 2 Step -1
        If n And 1 Then Mid$(LongToBits, i, 1) = &quot;1&quot;
        n = n \ 2
    Next
End Function

'------------------------------------------------------------------------------

MsgBox ByteToBits(0)			'&lt;--displays: 00000000000000000000000000000000
MsgBox LongToBits(293781237)	'&lt;--displays: 00010001100000101011111011110101
MsgBox ByteToBits(-1)			'&lt;--displays: 11111111111111111111111111111111

4 (9 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
vba convert to byte array excel formula binary to decimal excel binary to decimal 16 bit excel vba bits to long excel vba byte to bits how to convert decimal to binary in casio fx-82ms to byte array string vb.net binary to decimal excel excel binary to decimal 32 bit using bit mask in excel vba excel vba binary to hex excel binary to decimal excel vba check if integer has been set excel vba 32-bit long integer to bits excel vba 32bit long integer to bits excel vba long integer to bits xl vba long to binary excelvba long to binary excel vba long to binary excel vba long to bit string excel vba check if specific bits set in 64bit longlong integer vba check if specific bits set in longlong excel vba check if specific bits are set in 64-bit longlong integer excel vba check if certain bits are set in 64-bit longlong integer vba test if particular bits set in longlong vba test if particular bits set in longlong integer vba test if particular bits set in long integer vba test if certain bits set in long excel vba test if certain bits set in long excel vba test if specific bits set in long excel vba check if specific bits set in long vba check if specific bits set in int32 excel excel vba check if specific bits set in int32 xlvba check if specific bits are set in int32 xl vba check if specific bits are set in int32 excel vba check if specific bits are set in int32 vba check if specific bits are set in int32 vba test if specific bits are set in int32 excel vba test if specific bits are set in long integer vba test if specific bits are set in long integer vba check if specific bits are set in long integer excel xl vba check if specific bits are set in long integer excel vba check if specific bits set in long integer vba check if specific bits set in long excel vba are bits set in long excel vba check if particular bits are set in integer vba check if particular bits are set in integer xlvba check if specific bits set in integer xl vba check if specific bits set in integer excelvba check if specific bits set in integer excelvba check if specific bits are set in integer excel vba check if specific bits are set in integer vba check if specific bits are set in integer excelvba test if specific bits are set in integer excel vba test if specific bits are set in integer xlvba test if specific bits set in integer excel vba test if specific bits set in integer vba test if specific bits set in integer vba are bits set in integer excel Check if bits are SET in vba Check if bits are SET in vba xlvba test if specific bits are set in byte excelvba test if specific bits are set in byte excel vba test if specific bits are set in byte vba test if specific bits are set in byte vba test if specific bits set in byte excelvba check if specific bits set in byte excel vba check if specific bits set in byte vba check if specific bits set in byte vba specific bits set in byte xl vba are specific bits set in byte excel vba are specific bits set in byte excel vba are bits set in byte excel vba check if bit is set in 64-bit longlong integer xl vba is bit set in 64-bit longlong integer excel vba is bit set in longlong integer vba check if bit set in longlong integer vba check if specific bit set in longlong integer xl vba is specific bit set in long integer excelvba is specific bit set in long integer xlvba check if bit is set in 32-bit long integer xl vba check if bit is set in 32-bit long integer excelvba check if bit is set in 32-bit long integer excel vba check if bit is set in 32-bit long integer vba check if bit is set in 32-bit long integer xlvba check if bit is set in long xl vba check if bit is set in long excelvba check if bit is set in long excel vba check if bit is set in long vba check if bit is set in long vba if bit is set long excelvba is bit set long xlvba is bit set long xl vba is bit set long vba is bit set long excel vba is bit set long excel vba is bit set integer vba is bit set integer vba is bit set in integer xlvba check integer bit xl vba check integer bit excelvba check integer bit excel vba check integer bit vba check integer bit xlvba check if bit set in integer xl vba check if bit set in integer vba check if bit set in integer excelvba check if bit set in integer excel vba check if bit set in integer excel vba integer is bit set excel vba is bit set in integer vba check if bit is set in integer xvba check if bit is set in integer xlvba check if bit is set in integer xl vba check if bit is set in integer excelvba check if bit is set in integer excel vba check if bit is set in integer xl vba check if bit is set in byte xlvba check if bit is set in byte excelvba check if bit is set in byte excel vba check if bit is set in byte vba check if bit is set in byte xlvba bit is set in byte xl vba bit is set in byte excelvba bit is set in byte excel vba bit is set in byte vba bit is set in byte excel vba is bit set in byte vba is bit set in byte xlvba set bit in byte xl vba set bit in byte excelvba set bit in byte excel vba set bit in byte excel vba bits to integer excelvba binary to decimal 8-bits excel vba binary to decimal 8-bits xl vba binary to decimal 8-bits xlvba binary to decimal 8-bits vba binary to decimal 8-bits xlvba binary to decimal 8bits xl vba binary to decimal 8bits excel vba binary to decimal 8bits vba binary to decimal 8bits excelvba binary to decimal 8bits excelvba binary to byte 8bits excel vba binary to byte 8bits xl vba binary to byte 8bits xlvba binary to byte 8bits vba binary to byte 8bits xl vba binary to byte xlvba binary to byte excelvba binary to byte excel vba binary to byte vba binary to byte excelvba bit string to byte excel vba bit string to byte xl vba bit string to byte xlvba bit string to byte vba bit string to byte xlvba bits to byte xl vba bits to byte excel vba bits to byte excelvba bits to byte vba bits to byte vba bitstobyte vb if system.byte data type vb.net byte 255 + 1 single long double vba vba long excel-vba long to bits excelvba long to bits excel vba long to bits vba create a bit mask excel-vba create a bit mask excelvba create a bit mask excel-vba bitmask excelvba bitmask excel vba bitmask vba bit mask excel-vba bit mask excelvba bit mask excel vba bit mask excel-vba convert byte value to bits excelvba convert byte value to bits excel vba convert byte value to bits vba binary from byte value excel-vba binary from byte value excelvba binary from byte value excel vba binary from byte value
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source