VBspeed / Bits / BitToLong
VBspeed © 2000-10, updated: 30-Dec-2000
BitToLong


Function BitToLong
Returns the Long value corresponding to a 32-char bit string (= 32 bit, most significant bit left).
For example: BitToLong01("00000000000000000000000000010111") --> 23
Code
BitToLong01
Doping: needs reference to StringHelpers typelib Split03.tlb (by Egbert Nierop)
Download TLB_Split03.zip (3KB zipped, VB5-compatible).

Public Function BitToLong01(bitexpr As String) As Long ' by Egbert Nierop, egbert_nierop@goovy.hotmail.com, 20001222 ' needs a reference to split03.tlb 'unicode fixed-length array Dim t(31) As Integer Dim lLen As Long lLen = Len(bitexpr) If lLen <> 32 Then Exit Function StringHelpers.kernel.MoveMemoryFromStr t(0), bitexpr, 64 For lLen = 1 To 31 BitToLong01 = 2 * BitToLong01 + (t(lLen) Xor 48) Next 'must do this at last to get rid of overflow in ide mode If (t(0) Xor 48) = 1 Then BitToLong01 = BitToLong01 Or &H80000000 End If End Function
BitToLong02
Public Function BitToLong02(bitexpr As String) As Long
' by Donald, donald@xbeat.net, 20001228
' based on BitToLong01 by Egbert Nierop, egbert_nierop@goovy.hotmail.com, 20001222
    Static abBit() As Byte
    Dim i As Long

    If LenB(bitexpr) <> 64 Then Exit Function
    abBit = bitexpr
    For i = 2 To 63 Step 2
        BitToLong02 = 2 * BitToLong02 Or (abBit(i) Xor 48)
    Next
    'must do this at last to get rid of overflow in ide mode
    If (abBit(0) Xor 48) Then
        BitToLong02 = BitToLong02 Or &H80000000
    End If
End Function
  
BitToLong03
Doping: needs reference to typelib Split03.tlb (by Egbert Nierop)
Download TLB_Split03.zip (3KB zipped, VB5-compatible).

Public Function BitToLong03(bitexpr As String) As Long ' by Egbert Nierop, egbert_nierop@goovy.hotmail.com, 20001228 ' inspired by BitToLong02 by Donald, donald@xbeat.net, 20001228 ' needs a reference to split03.tlb 'unicode fixed-length array Static t(31) As Integer Dim lLen As Long lLen = Len(bitexpr) If lLen <> 32 Then Exit Function StringHelpers.kernel.MoveMemoryFromStr t(0), bitexpr, 64 For lLen = 1 To 31 BitToLong03 = 2 * BitToLong03 + (t(lLen) Xor 48) Next 'must do this at last to get rid of overflow in ide mode If (t(0) Xor 48) = 1 Then BitToLong03 = BitToLong03 Or &H80000000 End If End Function
BitToLong04
Private Declare Sub RtlMoveMemory Lib "kernel32" ( _
    dst As Any, src As Any, ByVal nBytes&)

Public Function BitToLong04(bitexpr As String) As Long ' by G.Beckmann, G.Beckmann@NikoCity.de, 20001230 ' based on BitToLong03 by Egbert Nierop, egbert_nierop@goovy.hotmail.com, 20001228 Static t%(31): Dim asc0% If Len(bitexpr) <> 32 Then Exit Function RtlMoveMemory t(0), ByVal StrPtr(bitexpr), 64 asc0 = KeyCodeConstants.vbKey0 BitToLong04 = t(1) - asc0 BitToLong04 = 2 * BitToLong04 + t(2) - asc0 BitToLong04 = 2 * BitToLong04 + t(3) - asc0 BitToLong04 = 2 * BitToLong04 + t(4) - asc0 BitToLong04 = 2 * BitToLong04 + t(5) - asc0 BitToLong04 = 2 * BitToLong04 + t(6) - asc0 BitToLong04 = 2 * BitToLong04 + t(7) - asc0 BitToLong04 = 2 * BitToLong04 + t(8) - asc0 BitToLong04 = 2 * BitToLong04 + t(9) - asc0 BitToLong04 = 2 * BitToLong04 + t(10) - asc0 BitToLong04 = 2 * BitToLong04 + t(11) - asc0 BitToLong04 = 2 * BitToLong04 + t(12) - asc0 BitToLong04 = 2 * BitToLong04 + t(13) - asc0 BitToLong04 = 2 * BitToLong04 + t(14) - asc0 BitToLong04 = 2 * BitToLong04 + t(15) - asc0 BitToLong04 = 2 * BitToLong04 + t(16) - asc0 BitToLong04 = 2 * BitToLong04 + t(17) - asc0 BitToLong04 = 2 * BitToLong04 + t(18) - asc0 BitToLong04 = 2 * BitToLong04 + t(19) - asc0 BitToLong04 = 2 * BitToLong04 + t(20) - asc0 BitToLong04 = 2 * BitToLong04 + t(21) - asc0 BitToLong04 = 2 * BitToLong04 + t(22) - asc0 BitToLong04 = 2 * BitToLong04 + t(23) - asc0 BitToLong04 = 2 * BitToLong04 + t(24) - asc0 BitToLong04 = 2 * BitToLong04 + t(25) - asc0 BitToLong04 = 2 * BitToLong04 + t(26) - asc0 BitToLong04 = 2 * BitToLong04 + t(27) - asc0 BitToLong04 = 2 * BitToLong04 + t(28) - asc0 BitToLong04 = 2 * BitToLong04 + t(29) - asc0 BitToLong04 = 2 * BitToLong04 + t(30) - asc0 BitToLong04 = t(31) - asc0 + 2 * BitToLong04 If t(0) <> asc0 Then BitToLong04 = BitToLong04 Or &H80000000 End Function
Calls
1sRet = BitToLong("00000000000000000000000000000000") --> &H0
2sRet = BitToLong("10101010101010101010101010101010") --> &HAAAAAAAA
3sRet = BitToLong("11111111111111111111111111111111") --> &HFFFFFFFF (-1)
Charts
 VB5 Charts
CodeAuthorDopingNotes
BitToLong01 EgbertTLB 
BitToLong02 Donald  
BitToLong03 EgbertTLB 
BitToLong04 GuidoAPI 
Call 1
37.824.792µs
49.445.784µs
21.190.728µs
11.000.613µs
Call 2
38.034.844µs
410.146.119µs
21.210.730µs
11.000.604µs
Call 3
37.934.788µs
49.895.972µs
21.210.730µs
11.000.604µs
 VB6 Charts
CodeAuthorDopingNotes
BitToLong01 EgbertTLB 
BitToLong02 Donald  
BitToLong03 EgbertTLB 
BitToLong04 GuidoAPI 
Call 1
39.145.730µs
410.116.336µs
21.150.718µs
11.000.627µs
Call 2
39.035.677µs
49.606.036µs
21.150.722µs
11.000.629µs
Call 3
39.265.825µs
49.786.151µs
21.150.725µs
11.000.629µs
Conclusions
BitToLong03 vs BitToLong01: the Static (... t(31) As Integer) makes a lotta difference!
Got comments? How to read all those numbers

top




VBspeed © 2000-10 by Donald Lessau