# Thread: binary conversions to octal example

1. ## binary conversions to octal example

Hello again CL, today I have finished my binary conversion part of my program and have tackled all three conversions in one function. This functions speed efficiency is bigO(0), which again, is the fastest runtime. The reason I wanted to post this is because all over the net I see people take long ass ways just to convert a binary number to a decimal and I am like, "Wtf? Why are they making it harder than it has to be?".

All I need to do with this code now is make an error check for the input not being binary.
Code:
```//fnc to convert binary to all conversions, octal being the highest
void binaryToDecimal(int conversion) {
int dec=0; //dec result
int binary; //user input binary
int bits=64; //64bit binary

system("cls");
cout << "Input binary digit: ";
cin >> binary;

cout << endl;

//loop for binary math conversion
for (int i=0; i<bits; i++) {
//if remainder equals 1 then add 1(2^i)
if (binary%10==1)
dec=dec+1*(int)pow(2, i);

//cut 10 off from the binary
binary=binary/10;
}

if (conversion==0) //decimal
cout << "resultant: " << dec;
else if (conversion==1) //hex
cout << "resultant: " << "0x" << hex << dec;
else //octal
cout << "resultant: " << oct << dec;

cout << endl;
cout << endl;

system("pause");
system("cls");
}```
Behind the thinking:

In binary there are only two numbers who are 1 and 0. So this also would mean there are only two remainders when divided by 10, who are also 1 and 0. for every 1 we come across we add 1(2^i), since binary is based off of powers of two. For every 0 we come across we add 0(2^i) and since anything times 0 = zero(nothing) we would technically do nothing for the zeros we come across, basically skipping them.

1

2. Sad to see someone who knows what they're doing get no response.

1

3. Originally Posted by Yuri
Sad to see someone who knows what they're doing get no response.
Thanks + BUMP

0

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
All times are GMT -10. The time now is 04:57 PM.