Here's a speed challenge: let's see who can code the fastest algorithms to convert longs to binary strings and vice versa. Must work with negative numbers.
I'll post my code in a minute when I finish.
If that was at all unclear, I mean something like convert 5 to 00000000000000000000000000000101. Your option whether or not to chop off leading zeros.
I haven't tested The Hobo's yet, because he said he's working on it. Daok's is slower than mine, but Nucleus' function is about 3x faster. Lemme just do something to make it work with negatives....
VB Code:
Public Function DecToBin(ByVal X As Long) As String
DecToBin = "00000000000000000000000000000000"
If (X And 1) Then Mid$(DecToBin, 32) = "1"
If (X And 2) Then Mid$(DecToBin, 31) = "1"
If (X And 4) Then Mid$(DecToBin, 30) = "1"
If (X And 8) Then Mid$(DecToBin, 29) = "1"
If (X And 16) Then Mid$(DecToBin, 28) = "1"
If (X And 32) Then Mid$(DecToBin, 27) = "1"
If (X And 64) Then Mid$(DecToBin, 26) = "1"
If (X And 128) Then Mid$(DecToBin, 25) = "1"
If (X And 256) Then Mid$(DecToBin, 24) = "1"
If (X And 512) Then Mid$(DecToBin, 23) = "1"
If (X And 1024) Then Mid$(DecToBin, 22) = "1"
If (X And 2048) Then Mid$(DecToBin, 21) = "1"
If (X And 4096) Then Mid$(DecToBin, 20) = "1"
If (X And 8192) Then Mid$(DecToBin, 19) = "1"
If (X And 16384) Then Mid$(DecToBin, 18) = "1"
If (X And 32768) Then Mid$(DecToBin, 17) = "1"
If (X And 65536) Then Mid$(DecToBin, 16) = "1"
If (X And 131072) Then Mid$(DecToBin, 15) = "1"
If (X And 262144) Then Mid$(DecToBin, 14) = "1"
If (X And 524288) Then Mid$(DecToBin, 13) = "1"
If (X And 1048576) Then Mid$(DecToBin, 12) = "1"
If (X And 2097152) Then Mid$(DecToBin, 11) = "1"
If (X And 4194304) Then Mid$(DecToBin, 10) = "1"
If (X And 8388608) Then Mid$(DecToBin, 9) = "1"
If (X And 16777216) Then Mid$(DecToBin, 8) = "1"
If (X And 33554432) Then Mid$(DecToBin, 7) = "1"
If (X And 67108864) Then Mid$(DecToBin, 6) = "1"
If (X And 134217728) Then Mid$(DecToBin, 5) = "1"
If (X And 268435456) Then Mid$(DecToBin, 4) = "1"
If (X And 536870912) Then Mid$(DecToBin, 3) = "1"
If (X And 1073741824) Then Mid$(DecToBin, 2) = "1"
If (X And -2147483648#) Then Mid$(DecToBin, 1) = "1"
End Function
Also removed the if statement (it'd generate an overflow if it was greater than that before getting into the function anyway), and added the $ (which only makes it a very tiny bit faster).
I'm pretty sure that Nucleus' decimal to binary function is as fast as you can get; can anyone speed up the binary to decimal function I wrote from ideas from his code?
Time to start another thread: speed challenge for four functions: to and from hex with binary and decimal.
No offense intended, sorry if I caused any. Its just that this is a speed challenge; your code is shorter, but requires more time to do the conversion.
Daok didn't even have a chance. His code uses the very slow IIf function, and it lengthens a string in a loop. It's not really written with speed in mind. Also, it takes an integer and only processes a byte. Alphanos called for code that processes a Long.
double base2, base8, base10, base16, baseIn, baseConvert;
char bitChar;
int bitValue, bit, byteValue, base81, base82, base83;
int baseOfTwo[8]={1,2,4,8,16,32,64,128};
int baseofEight[8]={1,2,4,1,2,4,1,2};
int baseOfTen[8]={0,1,2,3,4,5,6,7};
int baseOfSixteen[8]={1,2,4,8,1,2,4,8};
void base2conv()
{
cout << "Enter an 8 bit value to convert: " << endl;
for (bit=7; bit >=0; bit --)
{
cin >> bitChar;
bitValue=bitChar - '0';
}
for (bit=0; bit <=2; bit++)
{
if (bitValue==1)
base81+=baseOfEight[bit];
}
for (bit=3; bit <=5; bit++)
{
if (bitValue==1)
base82+=baseOfEight[bit];
}
for (bit=6; bit <=7; bit++)
{
if (bitValue==1)
base83+=baseOfEight[bit];
}
cout << "The base 8 value is: " << base83 << base82 << base81 ;
}
cout << "What base would you like to convert to (8, 10, 16)?" << endl;
cin >> baseConvert;
if (baseConvert == 8)
{
int main()
{
cout << "Enter a base to be converted: " << endl;
cin >> baseIn;
if (baseIn == 2)
{
base2conv();
}
else if (baseIn == 8)
{
base8conv();
}
else if (baseIn == 10)
{
base10conv();
}
else if (baseIn ==16)
{
base16conv();
}
else
{
cout << "You have entered an invalid base. "
i had to write something like this for my C++ class, convert a binary number into a decimal or vice versa, the whole while accepting the inputs as strings. (We had to write our own string to binary/binary to string conversion class.)
Let me tell ya, it wasn't fun then, and it sure as hell wouldn't be fun now.
lol, I know what u mean, whered u think i got that from, pulled it from the closet
u probably did something like this....//oh boy I'm gonna be hated for this post
#include <iostream.h>
int baseOfEight[8]={1,2,4,1,2,4,1,2};
int baseOfTen[8]={1,2,4,8,16,32,64,128};
int baseOfSixteen[8]={1,2,4,8,1,2,4,8};
//base2 convert to base8
void base2conv8()
{
char bitChar;
int bitValue, bit;
int base81=0, base82=0, base83=0;
cout << "Enter an 8 bit value in base 2 form to convert: " << endl;
for (bit=7; bit >=6; bit --) // first 2 bits
{
cin >> bitChar;
bitValue=bitChar - '0';
if (bitValue==1)
base83+=baseOfEight[bit];
}
for (bit=5; bit >=3; bit--) // next 3 bits
{
cin >> bitChar;
bitValue=bitChar -'0';
if (bitValue==1)
base82+=baseOfEight[bit];
}
for (bit=2; bit >=0; bit--) // last 3 bits
{
cin >> bitChar;
bitValue=bitChar -'0';
if (bitValue==1)
base81+=baseOfEight[bit];
}
cout << "The base 8 value is: " << base83 << base82 << base81 ;
}
//base2 convert to base10
void base2conv10()
{
char bitChar=0;
int bitValue=0, bit=0, byteValue=0;
cout << "Enter an 8 bit value in base 2 form to convert: " << endl;
for (bit=7; bit >=0; bit --) // counts all the bits and multiplies the
{ // ones with value 1 with corresponding value
cin >> bitChar; // in baseOfTen function and then sums them
bitValue=bitChar - '0'; // in the variable byteValue
if (bitValue==1)
byteValue+=baseOfTen[bit];
}
cout << "The base 10 value is: " << byteValue << endl;
}
//base2 convert to base16
void base2conv16()
{
int base161=0, base162=0;
char bitChar;
int bitValue, bit;
cout << "Enter an 8 bit value in base 2 form to convert: " << endl;
for (bit=7; bit >=4; bit--) // Reads first 4 bits
{
cin >> bitChar;
bitValue=bitChar -'0'; // Converts to 0 or 1
if (bitValue==1) // if true
base162+=baseOfSixteen[bit]; //adds the sum of 1*baseOfSixteen[bit[
switch (base162) // Converts values >= 10 to correct hex value
{
case 10: base162='A'; break;
case 11: base162='B'; break;
case 12: base162='C'; break;
case 13: base162='D'; break;
case 14: base162='E'; break;
case 15: base162='F'; break;
}
}
for (bit=3; bit >=0; bit--)
{
cin >> bitChar;
bitValue=bitChar -'0';
if (bitValue==1)
base161+=baseOfSixteen[bit];
switch (base161)
{
case 10: base161='A'; break;
case 11: base161='B'; break;
case 12: base161='C'; break;
case 13: base161='D'; break;
case 14: base161='E'; break;
case 15: base161='F'; break;
}
}
if ((base162>9) && (base161>9)) //Test to see if type casting necessary
{
cout << "The base 16 value is: " << char(base162) << char(base161) << endl;
}
else if ((base162>9) && (base161<=9))
{
cout << "The base 16 value is: " << char(base162) << base161 << endl;
}
else if ((base162<=9) && (base161>9))
{
cout << "The base 16 value is: " << base162 << char(base161) << endl;
}
else
cout << "The base 16 value is: " << base162 << base161 << endl;
}
//base8 convert to base2; 8 to 2 to 10; 8 to 2 to 16
int base8conv2(int& test)
{
int base161=0, base162=0;
int bit=0, temp=0, bitValue=0, byteValue=0;
int base8num[3]={0,0,0}, base2num[8]={0,0,0,0,0,0,0,0};
cout << "Enter an 8 bit value in base 8 form to convert: " << endl;
for (bit=0; bit <=2; bit++)
{
cin >> base8num[bit];
}
//Tests all octal values
if (base8num[0]>3 || base8num[1]>7 || base8num[2]>7)
{
cout << "One of the values you entered was and invalid 8 bit base 8 value!" << endl;
cout << "Please quit and try again." << endl << endl;
return 0;
}
//base8 first 2 bits
base2num[7]=base8num[0]/2;
temp=base8num[0]%2;
base8num[0]=temp;
base2num[6]=base8num[0]/1;
else if (test==1) //Convert now 8 to 2 to 10
{
for (bit=7; bit>=0; bit--)
{
bitValue=base2num[bit];
if (bitValue==1)
byteValue+=baseOfTen[bit];
}
cout << "The base 10 value is: " << byteValue << endl;
return 0;
}
else if (test==2) //Convert now 8 to 2 to 16
{
for (bit=7; bit >=4; bit--) // Reads first 4 bits
{
bitValue=base2num[bit];
if (bitValue==1) // if true
base162+=baseOfSixteen[bit]; //adds the sum of 1*baseOfSixteen[bit[
switch (base162) // Converts values >= 10 to correct hex value
{
case 10: base162='A'; break;
case 11: base162='B'; break;
case 12: base162='C'; break;
case 13: base162='D'; break;
case 14: base162='E'; break;
case 15: base162='F'; break;
}
}
}
for (bit=3; bit >=0; bit--)
{
bitValue=base2num[bit];
if (bitValue==1)
base161+=baseOfSixteen[bit];
switch (base161)
{
case 10: base161='A'; break;
case 11: base161='B'; break;
case 12: base161='C'; break;
case 13: base161='D'; break;
case 14: base161='E'; break;
case 15: base161='F'; break;
}
}
if ((base162>9) && (base161>9)) //Test to see if type casting necessary
{
cout << "The base 16 value is: " << char(base162) << char(base161) << endl;
}
else if ((base162>9) && (base161<=9))
{
cout << "The base 16 value is: " << char(base162) << base161 << endl;
}
else if ((base162<=9) && (base161>9))
{
cout << "The base 16 value is: " << base162 << char(base161) << endl;
}
else
cout << "The base 16 value is: " << base162 << base161 << endl;
return 0;
}
//base10 convert to 2; base10 to 2 to 8; 10 to 2 to 16
int base10conv2(int& test)
{
int base161=0, base162=0;
int bit=0, temp=0, bitValue=0, byteValue=0;
int base2num[8]={0,0,0,0,0,0,0,0}, base10num;
int base81=0, base82=0, base83=0;
cout << "Enter an 8 bit value in base 10 form to convert: " << endl;
cin >> base10num;
//test for positive value
if (base10num<0)
{
cout<< "Base 10 value must be a positive value!" << endl;
return 0;
}
base2num[7]=base10num/128; //begin of base10 to 2 convert
temp=base10num%128;
base10num=temp;
base2num[6]=base10num/64;
temp=base10num%64;
base10num=temp;
base2num[5]=base10num/32;
temp=base10num%32;
base10num=temp;
base2num[4]=base10num/16;
temp=base10num%16;
base10num=temp;
base2num[3]=base10num/8;
temp=base10num%8;
base10num=temp;
base2num[2]=base10num/4;
temp=base10num%4;
base10num=temp;
base2num[1]=base10num/2;
temp=base10num%2;
base10num=temp;
base2num[0]=base10num/1;
else if (test==1) //Convert now 10 to 2 to 8
{
for (bit=7; bit >=6; bit --) // first 2 bits
{
bitValue=base2num[bit];
if (bitValue==1)
base83+=baseOfEight[bit];
}
for (bit=5; bit >=3; bit--) // next 3 bits
{
bitValue=base2num[bit];
if (bitValue==1)
base82+=baseOfEight[bit];
}
for (bit=2; bit >=0; bit--) // last 3 bits
{
bitValue=base2num[bit];
if (bitValue==1)
base81+=baseOfEight[bit];
}
cout << "The base 8 value is: " << base83 << base82 << base81 ;
return 0;
}
else if (test==2) //Convert now 10 from 2 to 16
{
for (bit=7; bit >=4; bit--) // Reads first 4 bits
{
bitValue=base2num[bit];
if (bitValue==1) // if true
base162+=baseOfSixteen[bit]; //adds the sum of 1*baseOfSixteen[bit[
switch (base162) // Converts values >= 10 to correct hex value
{
case 10: base162='A'; break;
case 11: base162='B'; break;
case 12: base162='C'; break;
case 13: base162='D'; break;
case 14: base162='E'; break;
case 15: base162='F'; break;
}
}
}
for (bit=3; bit >=0; bit--)
{
bitValue=base2num[bit];
if (bitValue==1)
base161+=baseOfSixteen[bit];
switch (base161)
{
case 10: base161='A'; break;
case 11: base161='B'; break;
case 12: base161='C'; break;
case 13: base161='D'; break;
case 14: base161='E'; break;
case 15: base161='F'; break;
}
}
if ((base162>9) && (base161>9)) //Test to see if type casting necessary
{
cout << "The base 16 value is: " << char(base162) << char(base161) << endl;
}
else if ((base162>9) && (base161<=9))
{
cout << "The base 16 value is: " << char(base162) << base161 << endl;
}
else if ((base162<=9) && (base161>9))
{
cout << "The base 16 value is: " << base162 << char(base161) << endl;
}
else
cout << "The base 16 value is: " << base162 << base161 << endl;
return 0;
}
Last edited by VBnewbC++er; Jan 14th, 2002 at 12:34 AM.
//base16 convert to 2; base16 to 2 to 8; 10 to 2 to 10
int base16conv2(int& test)
{
char base161=0, base162=0;
int bit=0, temp=0, bitValue=0, byteValue=0;
int base2num[8]={0,0,0,0,0,0,0,0};
int base81=0, base82=0, base83=0;
cout << "Enter an 8 bit value in base 16 form to convert: " << endl;
cin >> base162 >> base161;
switch (base162) //assigns 4 binary values corresponding to hex value
{
case '0': base2num[7]=0; base2num[6]=0; base2num[5]=0; base2num[4]=0; break;
case '1': base2num[7]=0; base2num[6]=0; base2num[5]=0; base2num[4]=1; break;
case '2': base2num[7]=0; base2num[6]=0; base2num[5]=1; base2num[4]=0; break;
case '3': base2num[7]=0; base2num[6]=0; base2num[5]=1; base2num[4]=1; break;
case '4': base2num[7]=0; base2num[6]=1; base2num[5]=0; base2num[4]=0; break;
case '5': base2num[7]=0; base2num[6]=1; base2num[5]=0; base2num[4]=1; break;
case '6': base2num[7]=0; base2num[6]=1; base2num[5]=1; base2num[4]=0; break;
case '7': base2num[7]=0; base2num[6]=1; base2num[5]=1; base2num[4]=1; break;
case '8': base2num[7]=1; base2num[6]=0; base2num[5]=0; base2num[4]=0; break;
case '9': base2num[7]=1; base2num[6]=0; base2num[5]=0; base2num[4]=1; break;
case 'A': base2num[7]=1; base2num[6]=0; base2num[5]=1; base2num[4]=0; break;
case 'B': base2num[7]=1; base2num[6]=0; base2num[5]=1; base2num[4]=1; break;
case 'C': base2num[7]=1; base2num[6]=1; base2num[5]=0; base2num[4]=0; break;
case 'D': base2num[7]=1; base2num[6]=1; base2num[5]=0; base2num[4]=1; break;
case 'E': base2num[7]=1; base2num[6]=1; base2num[5]=1; base2num[4]=0; break;
case 'F': base2num[7]=1; base2num[6]=1; base2num[5]=1; base2num[4]=1; break;
}
switch (base161)
{
case '0': base2num[3]=0; base2num[2]=0; base2num[1]=0; base2num[0]=0; break;
case '1': base2num[3]=0; base2num[2]=0; base2num[1]=0; base2num[0]=1; break;
case '2': base2num[3]=0; base2num[2]=0; base2num[1]=1; base2num[0]=0; break;
case '3': base2num[3]=0; base2num[2]=0; base2num[1]=1; base2num[0]=1; break;
case '4': base2num[3]=0; base2num[2]=1; base2num[1]=0; base2num[0]=0; break;
case '5': base2num[3]=0; base2num[2]=1; base2num[1]=0; base2num[0]=1; break;
case '6': base2num[3]=0; base2num[2]=1; base2num[1]=1; base2num[0]=0; break;
case '7': base2num[3]=0; base2num[2]=1; base2num[1]=1; base2num[0]=1; break;
case '8': base2num[3]=1; base2num[2]=0; base2num[1]=0; base2num[0]=0; break;
case '9': base2num[3]=1; base2num[2]=0; base2num[1]=0; base2num[0]=1; break;
case 'A': base2num[3]=1; base2num[2]=0; base2num[1]=1; base2num[0]=0; break;
case 'B': base2num[3]=1; base2num[2]=0; base2num[1]=1; base2num[0]=1; break;
case 'C': base2num[3]=1; base2num[2]=1; base2num[1]=0; base2num[0]=0; break;
case 'D': base2num[3]=1; base2num[2]=1; base2num[1]=0; base2num[0]=1; break;
case 'E': base2num[3]=1; base2num[2]=1; base2num[1]=1; base2num[0]=0; break;
case 'F': base2num[3]=1; base2num[2]=1; base2num[1]=1; base2num[0]=1; break;
}
if (test==0) //for conversion of 16 to 2
{
cout << "The base 2 form is: ";
cout << base2num[7] << base2num[6] << base2num[5] << base2num[4] << base2num[3]
<< base2num[2] << base2num[1] << base2num[0] << endl;
return 0;
}
else if (test==1) //Convert now 16 from 2 to 8
{
for (bit=7; bit >=6; bit --) // first 2 bits
{
bitValue=base2num[bit];
if (bitValue==1)
base83+=baseOfEight[bit];
}
for (bit=5; bit >=3; bit--) // next 3 bits
{
bitValue=base2num[bit];
if (bitValue==1)
base82+=baseOfEight[bit];
}
for (bit=2; bit >=0; bit--) // last 3 bits
{
bitValue=base2num[bit];
if (bitValue==1)
base81+=baseOfEight[bit];
}
cout << "The base 8 value is: " << base83 << base82 << base81 ;
return 0;
}
else if (test==2) //Convert now 16 from 2 to 10
{
for (bit=7; bit>=0; bit--)
{
bitValue=base2num[bit];
if (bitValue==1)
byteValue+=baseOfTen[bit];
}
cout << "The base 10 value is: " << byteValue << endl;
return 0;
}
return 0;
}
int main()
{
int baseIn, baseConv;
cout << "What base number do you wish to use? (2, 8, 10, 16)" << endl;
cin >> baseIn;
if (baseIn==2)
{
cout << "What base do you want to convert to? (8, 10, 16)" << endl;
cin >> baseConv;
if (baseConv==8)
{
base2conv8();
}
else if(baseConv==10)
{
base2conv10();
}
else if(baseConv==16)
{
base2conv16();
}
else
{
cout << "You entered an invalid base to convert to!" << endl;
return 0;
}
}
else if (baseIn==8)
{
cout << "What base do you want to convert to? (2, 10, 16)" << endl;
cin >> baseConv;
if (baseConv==2)
{
int test=0;
base8conv2(test);
}
else if(baseConv==10)
{
int test=1;
base8conv2(test);
}
else if(baseConv==16)
{
int test=2;
base8conv2(test);
}
else
{
cout << "You entered an invalid base to covert to!" << endl;
return 0;
}
}
else if (baseIn==10)
{
cout << "What base do you want to convert to? (2, 8, 16)" << endl;
cin >> baseConv;
if (baseConv==2)
{
int test=0;
base10conv2(test);
}
else if(baseConv==8)
{
int test=1;
base10conv2(test);
}
else if(baseConv==16)
{
int test=2;
base10conv2(test);
}
else
{
cout << "You entered an invalid base to covert to!" << endl;
return 0;
}
}
else if (baseIn==16)
{
cout << "What base do you want to convert to? (2, 8, 10)" << endl;
cin >> baseConv;
if (baseConv==2)
{
int test=0;
base16conv2(test);
}
else if(baseConv==8)
{
int test=1;
base16conv2(test);
}
else if(baseConv==10)
{
int test=2;
base16conv2(test);
}
else
{
cout << "You entered an invalid base to covert to!" << endl;
return 0;
}
}
else
{
cout << "You have entered an in valid base!" << endl;
return 0;
}
whewww, my great instructor gave us a whole week for that 2, lol sry bout the C++guys, I just picked up my first VB book today and ripped a version of Rose to play with
Yeah, we were given a week also...
Debugging it was hell too... i think i lost 4 points for poor comments in the code (hell i was lucky I got it submitted by the deadline)
I'll post my code if i can find it but im pretty sure I erased that %#$% as soon as the grade was in....
Since its two in the morning, I'm sure you'll forgive me if I try testing out C++ code sometime later. Just putting the files together and compiling them would take several minutes, and I have spent much less time on C++ than I have on VB, so I don't know how to make a timer to test the time it takes for a couple thousand iterations.
Anyway, here's my best effort. With 5 minutes in it, I can't brag. Just curious how fast it is (not very methinks...)
Code:
Private Function CBin(IntVal As Long)
Dim MaxPower As Integer
Dim i As Integer
Dim BinaryString As String
Do Until 2 ^ MaxPower >= IntVal
MaxPower = MaxPower + 1
Loop
Do Until MaxPower < 0
If (IntVal - (2 ^ MaxPower)) >= 0 Then
BinaryString = BinaryString & "1"
IntVal = IntVal - 2 ^ MaxPower
Else
BinaryString = BinaryString & "0"
End If
MaxPower = MaxPower - 1
Loop
CBin = BinaryString
End Function
Figured as much. Oh well, can't be bothered adding that ability - it's just a matter of changing a - to a + and modifying the appropriate bit.
And, here's my brother function, Bin to Dec:
Code:
Private Function ToDec(BinString As String)
Dim DecNum As Long
Dim i As Integer, j As Integer
j = Len(BinString)
For i = 1 To j
If Mid(BinString, i, 1) = "1" Then
DecNum = DecNum + 2 ^ (j - i)
End If
Next i
ToDec = DecNum
End Function
Originally posted by rjlohan
Figured as much. Oh well, can't be bothered adding that ability - it's just a matter of changing a - to a + and modifying the appropriate bit.
It would be lovely t'were it as easy as that. you cant just make a number negative by switching the leftmost bit... I posted something recently on signed integers to binary and back again.
From what I understand (or seem to remember, amidst a beer-haze) a negative-bit binary number operates on 7-bits per byte, where the 8th (leftmost) bit is +/- depending. Where did I go wrong?
(Oh that's right, I skipped lectures to go drink beer and watch Fight Club...)
I wouldn't mind getting my "dec to binary" code evaluated, although its more of a "String to Binary" process.
The Key Sub is Command1_Click, or:
VB Code:
Private Sub Command1_Click()
If Len(Text1.Text) = 0 Then
MsgBox "Sorry, Please enter some text to convert first!"
Exit Sub
End If
Dim MyStr As String
Dim i As Long
For i = 1 To Len(Text1.Text)
MyStr = MyStr & ARR_BIN(Asc(Mid(Text1.Text, i, 1)))
Next i
Text3.Text = MyStr
End Sub
The setup was done in Form_Load{I think} but once done, this thig is Speedy.
-Lou
PS:
'Course, its not converting Dec Numbers to Bin Numbers, So I guess, Ignore it. This is the wrong thread. But its the closest I've seen so far.
Last edited by NotLKH; Jan 14th, 2002 at 08:23 PM.