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