I know that Visual Basic has, and probably will always be, second to C++ when it comes to speed in bit calculations. Likewise, ASP (using VBScript) has, and probably will always be, second to PHP when it comes to speed in bit calculations. This functionalitiy, however, is needed for a project that I am working on. Since I am using ASP, optimization is also needed.
The basic idea was to create a function called CBit(), taking one ByVal parameter of type "byte" ("unsigned short int" in C++) (in theory - as VBScript uses all variants). The function would take that byte and output a string (char array) containing "1"s and "0"s in a series of bits. The value, of course, is returned by value.
I managed to whip up this little thing fairly quickly, but I'm sure that it could be made faster (don't get me wrong, it's very fast now - I just want to max it's speed).
CODE
Public Function CBit(number)
Dim exponent, buffer
exponent = 0
Do While (2 ^ exponent) * 2 - 1 < number
If exponent = 0 Then
exponent = exponent + 3
Else
exponent = exponent + 4
End If
Loop
Do
If 2 ^ exponent <= number Then
number = number - (2 ^ exponent)
buffer = buffer + "1"
Else
buffer = buffer + "0"
End If
If exponent > 0 Then
exponent = exponent - 1
Else
Exit Do
End If
Loop
CBit = buffer
End Function
Any suggestions . . . at all . . . would be greatly appreciated!