VBspeed / Bits / ByteToBit
VBspeed © 2000-10, updated: 01-Oct-2001
ByteToBit


Function ByteToBit
Returns an 8-char string representing the bit-pattern of a specified byte (most significant bit left).
For example: ByteToBit(3) => "00000011"
Code
ByteToBit01
Public Function ByteToBit01(b As Byte) As String
' by Donald, donald@xbeat.net, 20001206
  Dim i As Long
  
  ByteToBit01 = "00000000"
  
  For i = 0 To 7
    If b And 2 ^ i Then
      Mid$(ByteToBit01, 8 - i, 1) = "1"
    End If
  Next
  
End Function
  
ByteToBit02
Public Function ByteToBit02(b As Byte) As String
' by Donald, donald@xbeat.net, 20001206
  
  ByteToBit02 = "00000000"
  
  If b And 1 Then Mid$(ByteToBit02, 8) = "1"
  If b And 2 Then Mid$(ByteToBit02, 7) = "1"
  If b And 4 Then Mid$(ByteToBit02, 6) = "1"
  If b And 8 Then Mid$(ByteToBit02, 5) = "1"
  If b And 16 Then Mid$(ByteToBit02, 4) = "1"
  If b And 32 Then Mid$(ByteToBit02, 3) = "1"
  If b And 64 Then Mid$(ByteToBit02, 2) = "1"
  If b And 128 Then Mid$(ByteToBit02, 1) = "1"
  
End Function
  
ByteToBit03
Public Function ByteToBit03(b As Byte) As String
' by Donald, donald@xbeat.net, 20001206
  
  ByteToBit03 = "00000000"
  
  If b And 1 Then Mid$(ByteToBit03, 8, 1) = "1"
  If b And 2 Then Mid$(ByteToBit03, 7, 1) = "1"
  If b And 4 Then Mid$(ByteToBit03, 6, 1) = "1"
  If b And 8 Then Mid$(ByteToBit03, 5, 1) = "1"
  If b And 16 Then Mid$(ByteToBit03, 4, 1) = "1"
  If b And 32 Then Mid$(ByteToBit03, 3, 1) = "1"
  If b And 64 Then Mid$(ByteToBit03, 2, 1) = "1"
  If b And 128 Then Mid$(ByteToBit03, 1, 1) = "1"
  
End Function
  
ByteToBit04
Public Function ByteToBit04(b As Byte) As String
' by Donald, donald@xbeat.net, 20001227
  Static abBits(15) As Byte
  abBits(14) = (b And 1) + 48
  abBits(12) = (b \ 2 And 1) + 48
  abBits(10) = (b \ 4 And 1) + 48
  abBits(8) = (b \ 8 And 1) + 48
  abBits(6) = (b \ 16 And 1) + 48
  abBits(4) = (b \ 32 And 1) + 48
  abBits(2) = (b \ 64 And 1) + 48
  abBits(0) = (b \ 128) + 48
  ByteToBit04 = abBits
End Function
  
ByteToBit05
Public Function ByteToBit05(ByVal b As Long) As String
' by Donald, donald@xbeat.net, 20001227
  Static abBits(15) As Byte
  Dim i As Long
  
  For i = 14 To 0 Step -2
    abBits(i) = (b And 1) + 48
    b = b \ 2
  Next
  ByteToBit05 = abBits
End Function
  
ByteToBit06
Public Function ByteToBit06(ByVal b As Long) As String
' by Egbert Nierop, egbert_nierop@goovy.hotmail.com, 20001221
  Dim cx As Long
  
  ByteToBit06 = "00000000"
 
  For cx = 8 To 1 Step -1
    If b And 1 Then Mid$(ByteToBit06, cx, 1) = "1"
    b = b \ 2 'shift right
  Next
  
End Function
  
ByteToBit07
Public Function ByteToBit07(b As Byte) As String
' by Donald, donald@xbeat.net, 20001221
  Dim i As Long
  Dim bit As Long
    
  ByteToBit07 = "00000000"
  bit = 1
  
  For i = 8 To 1 Step -1
    If b And bit Then Mid$(ByteToBit07, i, 1) = "1"
    bit = bit * 2
  Next
  
End Function
  
ByteToBit08
Public Static Function ByteToBit08(b As Byte) As String
' by Peter Nierop, pnierop.pnc@inter.nl.net, 20001223
  Dim lDone&, sNibble(0 To 15) 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"
    lDone = 1
  End If
  ByteToBit08 = sNibble(b \ 16 And &HF) & sNibble(b And &HF)
End Function
  
ByteToBit09
Public Function ByteToBit09(ByVal b As Long) As String
' by Jost Schwider, jost@schwider.de, 20001227
  Dim i As Long
  
  ByteToBit09 = "00000000"
  i = 15
  Do While b
    If b And 1 Then MidB$(ByteToBit09, i) = "1"
    b = b \ 2
    i = i - 2
  Loop
End Function
  
ByteToBit10
Public Function ByteToBit10(ByVal b As Long) As String
' by Jost Schwider, jost@schwider.de, 20001228, rev 001
' based on ByteToBit02 by Donald, donald@xbeat.net, 20001206
  ByteToBit10 = "00000000"
  If b And &H1& Then MidB$(ByteToBit10, 15&) = "1"
  If b And &H2& Then MidB$(ByteToBit10, 13&) = "1"
  If b And &H4& Then MidB$(ByteToBit10, 11&) = "1"
  If b And &H8& Then MidB$(ByteToBit10, 9&) = "1"
  If b And &H10& Then MidB$(ByteToBit10, 7&) = "1"
  If b And &H20& Then MidB$(ByteToBit10, 5&) = "1"
  If b And &H40& Then MidB$(ByteToBit10, 3&) = "1"
  If b And &H80& Then MidB$(ByteToBit10, 1&) = "1"
End Function
  
ByteToBit11
Public Static Function ByteToBit11(ByVal b As Long) As String
' by Jost Schwider, jost@schwider.de, 20001227
  Dim i As Long
  Dim sByte(0 To 255) As String
  
  If i Then
    ByteToBit11 = sByte(b)
  Else
    For i = 0 To 255
      sByte(i) = ByteToBit10(i)
    Next i
    ByteToBit11 = sByte(b)
  End If
End Function
  
ByteToBit12
Public Static Function ByteToBit12(ByVal b As Long) As String
' by Peter Weighill, pweighill@btinternet.com, 20010101
' based on ByteToBit11 by Jost Schwider, jost@schwider.de, 20001227
  Dim sByte(0 To 255) As String

  If LenB(sByte(b)) = 0 Then
    sByte(b) = ByteToBit10(b)
  End If
  ByteToBit12 = sByte(b)
End Function
  
ByteToBit13
Public Static Function ByteToBit13(ByVal b As Long) As String
' by Donald, donald@xbeat.net, 20010102
' based on ByteToBit11 by Jost Schwider, jost@schwider.de, 20001227
  Dim i As Long
  Dim sByte(0 To 255) As String * 8

  If i Then
    ByteToBit13 = sByte(b)
  Else
    For i = 0 To 255
      sByte(i) = ByteToBit10(i)
    Next i
    ByteToBit13 = sByte(b)
  End If
End Function
  
Calls
1sRet = ByteToBit(&H0) --> "00000000"
2sRet = ByteToBit(&HAA) --> "10101010"
3sRet = ByteToBit(&HFF) --> "11111111"
4sRet = ByteToBit([0 to 255]) --> all possible values
Charts
 VB5 Charts
CodeAuthorDopingNotes
ByteToBit01 Donald  
ByteToBit02 Donald  
ByteToBit03 Donald  
ByteToBit04 Donald  
ByteToBit05 Donald  
ByteToBit06 Egbert  
ByteToBit07 Donald  
ByteToBit08 PeterNierop  
ByteToBit09 Jost  
ByteToBit10 Jost  
ByteToBit11 Jost  
ByteToBit12 PeterW  
ByteToBit13 Donald  
Call 1
1313.0413.863µs
21.011.068µs
31.011.076µs
112.092.220µs
122.162.300µs
91.211.283µs
71.131.196µs
101.341.421µs
11.001.063µs
41.021.079µs
61.081.151µs
81.171.243µs
51.041.109µs
Call 2
1313.3814.717µs
81.721.886µs
71.701.868µs
112.022.222µs
122.092.300µs
101.942.128µs
91.832.013µs
41.281.408µs
61.681.845µs
51.531.688µs
21.141.258µs
31.181.299µs
11.001.100µs
Call 3
1314.1715.563µs
102.432.672µs
92.432.671µs
52.032.235µs
62.102.303µs
122.662.919µs
112.612.870µs
41.301.424µs
82.242.457µs
72.132.334µs
31.151.258µs
21.111.224µs
11.001.098µs
Call 4
1313.5514.849µs
71.751.914µs
61.731.891µs
92.032.222µs
122.142.343µs
102.052.244µs
112.052.245µs
41.301.426µs
81.801.973µs
51.611.769µs
21.141.247µs
31.261.385µs
11.001.096µs
 VB6 Charts
CodeAuthorDopingNotes
ByteToBit01 Donald  
ByteToBit02 Donald  
ByteToBit03 Donald  
ByteToBit04 Donald  
ByteToBit05 Donald  
ByteToBit06 Egbert  
ByteToBit07 Donald  
ByteToBit08 PeterNierop  
ByteToBit09 Jost  
ByteToBit10 Jost  
ByteToBit11 Jost  
ByteToBit12 PeterW  
ByteToBit13 Donald  
Call 1
1313.6813.877µs
41.041.057µs
51.041.059µs
111.961.990µs
122.042.071µs
101.261.283µs
81.161.180µs
91.221.239µs
21.021.034µs
71.071.084µs
11.001.014µs
31.031.047µs
61.051.064µs
Call 2
1314.8215.003µs
112.322.345µs
92.232.259µs
51.911.932µs
62.032.055µs
122.432.464µs
102.312.343µs
41.221.237µs
82.062.087µs
72.062.084µs
11.001.013µs
21.041.048µs
31.061.071µs
Call 3
1315.8316.039µs
93.293.331µs
103.303.338µs
51.911.932µs
62.022.051µs
123.533.578µs
113.383.424µs
41.221.237µs
83.023.056µs
72.902.935µs
11.001.013µs
21.031.048µs
31.061.069µs
Call 4
1314.3115.163µs
102.212.344µs
92.142.264µs
51.841.947µs
71.982.098µs
122.452.593µs
112.382.527µs
41.171.235µs
82.132.257µs
61.962.073µs
11.001.060µs
31.051.113µs
21.041.102µs
Conclusions
  • ByteToBit13 employs fixed-length strings, successfully under VB5. VB6 remains unimpressed.
  • ByteToBit11 uses a local cache as ByteToBit08 but goes all the way: look up instead of calculate.
  • ByteToBit10 is the overall best non-cache solution.
  • ByteToBit08, the nibble way: clumsiness that can convince.
  • ByteToBit01 looks nice and performs bad.
  • ByteToBit02 vs ByteToBit03: explicitly stating the length in the Mid$-statement (ByteToBit03) does not show any significant effect. So, since it's shorter, ByteToBit02 is this winter's number one.
Got comments? How to read all those numbers

top




VBspeed © 2000-10 by Donald Lessau