vba bits to integer

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 LongToBits(0)			'<--displays: 00000000000000000000000000000000
MsgBox LongToBits(293781237)	'<--displays: 00010001100000101011111011110101
MsgBox LongToBits(-1)			'<--displays: 11111111111111111111111111111111

5
6

                                    'Extremely fast VBA function to convert a binary string to a 16-bit Integer:

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

'Example:

MsgBox BitsToInteger(&quot;1111111111111111&quot;)    '&lt;--displays: -1
MsgBox BitsToInteger(&quot;0111111111111111&quot;)    '&lt;--displays:  32767

5 (6 Votes)
0
4.2
10
Clock ZHONG 100 points

                                    'If n is a 4-byte Long Integer, the Low (Left) Word is:
If n And &amp;H8000&amp; Then
  	LoWord = n Or &amp;HFFFF0000
Else
    LoWord = n And &amp;HFFFF&amp;
End If

'If n is a 4-byte Long Integer, the High (Right) Word is:
HiWord = (n And &amp;HFFFF0000) \ &amp;H10000

4.2 (10 Votes)
0
0
0

                                    Public Function MakeInteger%(LoByte As Byte, HiByte As Byte)
  If HiByte And &amp;H80 Then
    MakeInteger = ((HiByte * &amp;H100&amp;) Or LoByte) Or &amp;HFFFF0000
  Else
    MakeInteger = (HiByte * &amp;H100) Or LoByte
  End If
End Function

0
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
excel vba bits to long excel vba binary to hex vba value to integer vba cell value to integer excel vba 32bit long integer to bits vba long integer to bits xl vba long to binary excelvba long to binary excel vba long to binary vba long to bit string xl vba check if bit is set in 64-bit longlong integer excelvba check if bit is set in 64-bit longlong integer xl vba is bit set in 64-bit longlong integer excelvba is bit set in 64-bit integer xlvba is specific bit set in long integer excel vba is specific bit set in long integer vba is specific bit set in long integer xl vba binary string to integer xlvba binary string to integer excelvba binary string to integer excel vba binary string to integer vba binary string to integer excelvba binary string to short integer excel vba binary string to short integer vba binary string to short integer vba binary binary to short integer xlvba binary bits to short integer xl vba binary bits to short integer excelvba binary bits to short integer excel vba binary bits to short integer vba binary bits to short integer vba binary bits to integer short vba binary to integer vba bits to integer xl vba bits to integer excel vba bitstointeger vba bits to integer vba integer data type excel vba datatypes long in excel vba double vs long single long double vba vba difference between int and long integer limit in vba biggest long on vb data types vba vba long office vba define integer number of bits long variable excel vba make integer from bytes outlook data type in vba large variable vba long vba as long vba data types in vba long in vba types vba vba bigint excel vba make integer from two bytes vba long to bits excel vba long to bit excelvba hi integer from long excel vba integer from 2 bytes excelvba long integer variable to bits excel vba long integer variable to bits excel vba long integer variable to bits excel excel vba long integer variable to bits vba long integer variable to bits excelvba long integer value to bits excel-vba long integer value to bits excel-vba long integer variable to bits excel-vba long integer to bits excelvba long integer to bits excel vba long integer to bits excel vba long value to bits excel vba long integer value to bits excelvba convert 2 bytes to an integer excel vba convert 2 bytes to an integer excel-vba make a short integer from two byte values excelvba make a short integer from two byte values excel vba make a short integer from two byte values excel ba make a short integer from two byte values vba make a short integer from two byte values excel-vba make a short integer from two bytes excelvba make a short integer from two bytes excel vba make a short integer from two bytes vba high word from Long Integer excel-vba high word from Long Integer excelvba high word from Long Integer excel vba high word from Long Integer
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