VBspeed / Bits / LongToBit
VBspeed © 2000-10, updated: 30-Jul-2007
LongToBit


Function LongToBit
Returns a 32-char string representing the bit-pattern of a specified Long (= 4 bytes = 32 bit, most significant bit left).
For example: LongToBit(&HAAAAAAAA) --> "10101010101010101010101010101010"
Code
LongToBit01
Public Function LongToBit01(lLong As Long) As String
' by Donald, donald@xbeat.net, 20001206
  Dim i As Long
  
  LongToBit01 = String$(32, 48)    '48 = "0"
  
  For i = 0 To 30
    If lLong And 2 ^ i Then
      Mid$(LongToBit01, 32 - i) = "1"
    End If
  Next
  ' handle sign bit
  If lLong < 0 Then
    Mid$(LongToBit01, 1) = "1"
  End If
  
End Function
  
LongToBit02
Public Function LongToBit02(lLong As Long) As String
' by Donald, donald@xbeat.net, 20001206
  Const cONE As String = "1"
  
  LongToBit02 = String$(32, 48)    '48 = "0"
  
  If lLong And &H1& Then Mid$(LongToBit02, 32) = cONE
  If lLong And &H2& Then Mid$(LongToBit02, 31) = cONE
  If lLong And &H4& Then Mid$(LongToBit02, 30) = cONE
  If lLong And &H8& Then Mid$(LongToBit02, 29) = cONE
  If lLong And &H10& Then Mid$(LongToBit02, 28) = cONE
  If lLong And &H20& Then Mid$(LongToBit02, 27) = cONE
  If lLong And &H40& Then Mid$(LongToBit02, 26) = cONE
  If lLong And &H80& Then Mid$(LongToBit02, 25) = cONE
  
  If lLong And &H100& Then Mid$(LongToBit02, 24) = cONE
  If lLong And &H200& Then Mid$(LongToBit02, 23) = cONE
  If lLong And &H400& Then Mid$(LongToBit02, 22) = cONE
  If lLong And &H800& Then Mid$(LongToBit02, 21) = cONE
  If lLong And &H1000& Then Mid$(LongToBit02, 20) = cONE
  If lLong And &H2000& Then Mid$(LongToBit02, 19) = cONE
  If lLong And &H4000& Then Mid$(LongToBit02, 18) = cONE
  If lLong And &H8000& Then Mid$(LongToBit02, 17) = cONE
  
  If lLong And &H10000 Then Mid$(LongToBit02, 16) = cONE
  If lLong And &H20000 Then Mid$(LongToBit02, 15) = cONE
  If lLong And &H40000 Then Mid$(LongToBit02, 14) = cONE
  If lLong And &H80000 Then Mid$(LongToBit02, 13) = cONE
  If lLong And &H100000 Then Mid$(LongToBit02, 12) = cONE
  If lLong And &H200000 Then Mid$(LongToBit02, 11) = cONE
  If lLong And &H400000 Then Mid$(LongToBit02, 10) = cONE
  If lLong And &H800000 Then Mid$(LongToBit02, 9) = cONE
  
  If lLong And &H1000000 Then Mid$(LongToBit02, 8) = cONE
  If lLong And &H2000000 Then Mid$(LongToBit02, 7) = cONE
  If lLong And &H4000000 Then Mid$(LongToBit02, 6) = cONE
  If lLong And &H8000000 Then Mid$(LongToBit02, 5) = cONE
  If lLong And &H10000000 Then Mid$(LongToBit02, 4) = cONE
  If lLong And &H20000000 Then Mid$(LongToBit02, 3) = cONE
  If lLong And &H40000000 Then Mid$(LongToBit02, 2) = cONE
  If lLong And &H80000000 Then Mid$(LongToBit02, 1) = cONE
  
End Function
  
LongToBit03
Public Function LongToBit03(lLong As Long) As String
' by Donald, donald@xbeat.net, 20001206
  Static alPow2(0 To 31) As Long
  Dim i As Long
  
  ' initialize lookup table
  If alPow2(0) = 0 Then
    alPow2(0) = 1
    For i = 1 To 30
      alPow2(i) = alPow2(i - 1) * 2
    Next
    alPow2(31) = &H80000000
  End If
  
  LongToBit03 = String$(32, 48)    '48 = "0"
  
  For i = 0 To 31
    If lLong And alPow2(i) Then
      Mid$(LongToBit03, 32 - i) = "1"
    End If
  Next
  
End Function
  
LongToBit04
Public Function LongToBit04(ByVal lLong As Long) As String
' by Don/Egbert, 20001222
'    donald@xbeat.net
'    egbert_nierop@goovy.hotmail.com
  Dim i As Long
    
  LongToBit04 = String$(32, 48)    '48 = "0"
  
  ' handle sign bit
  If lLong And &H80000000 Then
    Mid$(LongToBit04, 1, 1) = "1"
    lLong = lLong And Not &H80000000
  End If
  
  For i = 32 To 2 Step -1
    If lLong And 1 Then Mid$(LongToBit04, i, 1) = "1"
    lLong = lLong \ 2 'shift right
  Next
  
End Function
  
LongToBit05
Doping: needs reference to typelib Split03.tlb (by Egbert Nierop)
Download TLB_Split03.zip (3KB zipped, VB5-compatible).

Public Function LongToBit05(ByVal lLong As Long) As String ' by Don/Egbert Nierop, egbert_nierop@goovy.hotmail.com, 20001227, rev 001 ' needs a reference to split03.tlb Dim lSptr As Long Static t(31) As Integer 'clear sign bit If lLong And &H80000000 Then lLong = lLong Xor &H80000000 t(0) = 49 Else t(0) = 48 End If For lSptr = 31 To 1 Step -1 t(lSptr) = (lLong And 1&) Xor 48 lLong = lLong \ 2 'shift right Next LongToBit05 = StringHelpers.SysAllocStringLen(t(0), 32) End Function
LongToBit06
Doping: needs reference to typelib Split03.tlb (by Egbert Nierop)
Download TLB_Split03.zip (3KB zipped, VB5-compatible).

Public Function LongToBit06(ByVal lLong As Long) As String ' by Egbert Nierop, egbert_nierop@goovy.hotmail.com, 20001226 ' needs a reference to split03.tlb Dim lSptr As Long LongToBit06 = StringHelpers.SysAllocStringLen(ByVal 0&, 32) lSptr = StrPtr(LongToBit06) 'clear sign bit If lLong And &H80000000 Then lLong = lLong Xor &H80000000 StringHelpers.kernel.FillMemory ByVal lSptr, 1, 49 Else StringHelpers.kernel.FillMemory ByVal lSptr, 1, 48 End If For lSptr = lSptr + 62 To lSptr + 2 Step -2 StringHelpers.kernel.FillMemory ByVal lSptr, 1, lLong And 1 Xor 48 lLong = lLong \ 2 'shift right Next End Function
LongToBit07
Public Static Function LongToBit07(l As Long) As String
' by Peter Nierop, pnierop.pnc@inter.nl.net, 20001226
  Dim lDone&, sNibble(0 To 15) As String, sByte(0 To 255) As String
  If lDone = 0 Then
    sNibble(0) = "0000 "
    sNibble(1) = "0001 "
    sNibble(2) = "0010 "
    sNibble(3) = "0011 "
    sNibble(4) = "0100 "
    sNibble(5) = "0101 "
    sNibble(6) = "0110 "
    sNibble(7) = "0111 "
    sNibble(8) = "1000 "
    sNibble(9) = "1001 "
    sNibble(10) = "1010 "
    sNibble(11) = "1011 "
    sNibble(12) = "1100 "
    sNibble(13) = "1101 "
    sNibble(14) = "1110 "
    sNibble(15) = "1111 "
    For lDone = 0 To 255
      sByte(lDone) = sNibble(lDone \ &H10) & sNibble(lDone And &HF)
    Next
  End If

  If l < 0 Then
    LongToBit07 = sByte(128 + (l And &H7FFFFFFF) \ &H1000000 And &HFF) _
                & sByte((l And &H7FFFFFFF) \ &H10000 And &HFF) _
                & sByte((l And &H7FFFFFFF) \ &H100 And &HFF) _
                & sByte(l And &HFF)
  Else
    LongToBit07 = sByte(l \ &H1000000 And &HFF) _
                & sByte(l \ &H10000 And &HFF) _
                & sByte(l \ &H100 And &HFF) _
                & sByte(l And &HFF)
  End If

End Function
  
LongToBit08
Public Static Function LongToBit08(ByVal L As Long) As String
' by Peter Nierop, pnierop.pnc@inter.nl.net, 20001226
  Dim lDone&, sNibble(0 To 15) As String, sByte(0 To 255) As String
  If lDone = 0 Then
    sNibble(0) = "0000 "
    sNibble(1) = "0001 "
    sNibble(2) = "0010 "
    sNibble(3) = "0011 "
    sNibble(4) = "0100 "
    sNibble(5) = "0101 "
    sNibble(6) = "0110 "
    sNibble(7) = "0111 "
    sNibble(8) = "1000 "
    sNibble(9) = "1001 "
    sNibble(10) = "1010 "
    sNibble(11) = "1011 "
    sNibble(12) = "1100 "
    sNibble(13) = "1101 "
    sNibble(14) = "1110 "
    sNibble(15) = "1111 "
    For lDone = 0 To 255
      sByte(lDone) = sNibble(lDone \ &H10) & sNibble(lDone And &HF)
    Next
  End If

  
  If L < 0 Then
    L = L And &H7FFFFFFF
    LongToBit08 = sByte(128 + L \ &H1000000 And &HFF) _
                & sByte(L \ &H10000 And &HFF) _
                & sByte(L \ &H100 And &HFF) _
                & sByte(L And &HFF)
  Else
    LongToBit08 = sByte(L \ &H1000000 And &HFF) _
                & sByte(L \ &H10000 And &HFF) _
                & sByte(L \ &H100 And &HFF) _
                & sByte(L And &HFF)
  End If


End Function
  
LongToBit09
Doping: needs reference to typelib Split03.tlb (by Egbert Nierop)
Download TLB_Split03.zip (3KB zipped, VB5-compatible).

Public Function LongToBit09(ByVal lLong As Long) As String ' by Paul, wpsjr1@syix.com, 20010910 ' SysAllocStringLen in oleaut32 - needs a reference to a typelib (eg split03.tlb) Static bChars(63) As Byte ' not recreating the array each time saves a lot of time, cudos to Don/Egbert Dim bBits As Byte If lLong And &H80000000 Then lLong = lLong Xor &H80000000 bChars(0) = 49 Else bChars(0) = 48 End If bBits = lLong And 255 ' mask off the lo byte bChars(62) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(60) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(58) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(56) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(54) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(52) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(50) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(48) = (bBits And 1) Xor 48 lLong = lLong \ &H100 ' shift right 32 bits bBits = lLong And 255 bChars(46) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(44) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(42) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(40) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(38) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(36) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(34) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(32) = (bBits And 1) Xor 48 lLong = lLong \ &H100 bBits = lLong And 255 bChars(30) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(28) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(26) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(24) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(22) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(20) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(18) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(16) = (bBits And 1) Xor 48 lLong = lLong \ &H100 bBits = lLong And 255 bChars(14) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(12) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(10) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(8) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(6) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(4) = (bBits And 1) Xor 48 bBits = bBits \ 2 bChars(2) = (bBits And 1) Xor 48 ' This function is the rolled out version of Don and Egbert's loop _ ' it uses 8 bit registers, and the unsigned byte type _ ' which gains a little extra speed. ' Notice there are no cdq and sub instructions before the shift _ ' which is typical when dividing vb's signed numbers. ' here is a little of the asm listing: ' ; 202 :bChars(60) = (bBits And 1) Xor 48 ' mov bl, dl ; move bBits into the bl register ' and bl, 1 ; mask off the one bit ' xor bl, 48 ; if bl is one it becomes 49, otherwise its 48 ' mov BYTE PTR [ecx+60], bl ; store to the byte array ' ' ; 203 : bBits = bBits \ 2 ' ' shr dl, 1 ; fast division by shifting LongToBit09 = StringHelpers.SysAllocStringLen(bChars(0), 32) End Function
LongToBit10
Doping: needs reference to typelib Split03.tlb (by Egbert Nierop)
Download TLB_Split03.zip (3KB zipped, VB5-compatible).

Public Function LongToBit10(ByVal LongX As Long) As String ' by Gilad Rave, grv34@hotmail.com, 20070730 Static b(63) As Byte b(0) = ((LongX And &H80000000) \ &H80000000) Xor 48 b(2) = ((LongX And &H40000000) \ &H40000000) Xor 48 b(4) = ((LongX And &H20000000) \ &H20000000) Xor 48 b(6) = ((LongX And &H10000000) \ &H10000000) Xor 48 b(8) = ((LongX And &H8000000) \ &H8000000) Xor 48 b(10) = ((LongX And &H4000000) \ &H4000000) Xor 48 b(12) = ((LongX And &H2000000) \ &H2000000) Xor 48 b(14) = ((LongX And &H1000000) \ &H1000000) Xor 48 b(16) = ((LongX And &H800000) \ &H800000) Xor 48 b(18) = ((LongX And &H400000) \ &H400000) Xor 48 b(20) = ((LongX And &H200000) \ &H200000) Xor 48 b(22) = ((LongX And &H100000) \ &H100000) Xor 48 b(24) = ((LongX And &H80000) \ &H80000) Xor 48 b(26) = ((LongX And &H40000) \ &H40000) Xor 48 b(28) = ((LongX And &H20000) \ &H20000) Xor 48 b(30) = ((LongX And &H10000) \ &H10000) Xor 48 b(32) = ((LongX And &H8000&) \ &H8000&) Xor 48 b(34) = ((LongX And &H4000) \ &H4000) Xor 48 b(36) = ((LongX And &H2000) \ &H2000) Xor 48 b(38) = ((LongX And &H1000) \ &H1000) Xor 48 b(40) = ((LongX And &H800) \ &H800) Xor 48 b(42) = ((LongX And &H400) \ &H400) Xor 48 b(44) = ((LongX And &H200) \ &H200) Xor 48 b(46) = ((LongX And &H100) \ &H100) Xor 48 b(48) = ((LongX And &H80) \ &H80) Xor 48 b(50) = ((LongX And &H40) \ &H40) Xor 48 b(52) = ((LongX And &H20) \ &H20) Xor 48 b(54) = ((LongX And &H10) \ &H10) Xor 48 b(56) = ((LongX And &H8) \ &H8) Xor 48 b(58) = ((LongX And &H4) \ &H4) Xor 48 b(60) = ((LongX And &H2) \ &H2) Xor 48 b(62) = ((LongX And &H1) \ &H1) Xor 48 LongToBit10 = StringHelpers.SysAllocStringLen(b(0), 32) End Function
Calls
1sRet = LongToBit(&H0) --> "00000000000000000000000000000000"
2sRet = LongToBit(&HAAAAAAAA) --> "10101010101010101010101010101010"
3sRet = LongToBit(&HFFFFFFFF) --> "11111111111111111111111111111111"
Charts
 VB5 Charts
CodeAuthorDopingNotes
LongToBit01 Donald  
LongToBit02 Donald  
LongToBit03 Donald  
LongToBit04 Don/Egbert  
LongToBit05 Don/EgbertTLB 
LongToBit06 EgbertTLB 
LongToBit07 PeterNierop  
LongToBit08 PeterNierop  
LongToBit09 PaulTLB 
LongToBit10 GiladTLB 
Call 1
1051.8810.583µs
41.440.293µs
52.030.413µs
62.030.413µs
31.410.287µs
95.961.216µs
72.740.559µs
82.780.567µs
21.110.226µs
11.000.204µs
Call 2
1054.6611.190µs
64.160.852µs
84.941.010µs
74.780.979µs
31.420.291µs
95.931.214µs
42.750.563µs
52.760.566µs
21.110.227µs
11.000.205µs
Call 3
1057.0911.652µs
77.121.453µs
97.861.605µs
87.651.560µs
31.400.286µs
65.971.218µs
42.710.552µs
52.840.579µs
21.130.230µs
11.000.204µs
 VB6 Charts
CodeAuthorDopingNotes
LongToBit01 Donald  
LongToBit02 Donald  
LongToBit03 Donald  
LongToBit04 Don/Egbert  
LongToBit05 Don/EgbertTLB 
LongToBit06 EgbertTLB 
LongToBit07 PeterNierop  
LongToBit08 PeterNierop  
LongToBit09 PaulTLB 
LongToBit10 GiladTLB 
Call 1
1050.0510.384µs
41.560.323µs
52.040.424µs
61.990.413µs
31.370.284µs
95.831.211µs
72.850.592µs
82.840.590µs
21.150.239µs
11.000.207µs
Call 2
1055.8311.603µs
76.581.367µs
87.241.504µs
97.151.485µs
31.370.284µs
65.921.229µs
52.880.599µs
42.850.593µs
21.140.236µs
11.000.208µs
Call 3
1058.7112.247µs
711.862.474µs
912.362.579µs
812.202.544µs
31.370.285µs
65.791.208µs
42.880.601µs
52.910.607µs
21.140.237µs
11.000.209µs
Conclusions
LongToBit09 looks alright to me.
LongToBit07 is the best non-typelib version.
LongToBit01 is what you usually get in the books, ahem.
Got comments? How to read all those numbers

top




VBspeed © 2000-10 by Donald Lessau