1. ## binary problem

Hi there
I am a brand new in C++. What i knew so far is to open and do only cout and cin with the <iostream> libary.

Question: is anyone please give me a direction how to convert a binary to dicimal. for instance: 11111 need to convert in dicimal.
any help or ideal is greatly appreciated.
Thank a buch.

2. is as simple as this.

00000001 == 1
00000010 == 2
00000100 == 4
00001000 == 8
00010000 == 16
00011111 == 31

understand?

each succesive bit(eg. 0 or 1) increases it's value buy a mlitiple of 2.

3. I think there's a simpler method but this will do the job -

Code:
```#include <iostream>
#include <cmath>

using namespace std;

int main()
{

char store[32]={0};
unsigned int result=0;

cout << "Enter binary number: ";
cin >> store;

for(int i=strlen(store);i>=0;i--)
{
if(store[i]=='1')
result += pow(2,strlen(store)-i-1);
}

cout << result;

return 0;
}```

4. yes, but he needs to understand the concepts behind it such as how binary numbers work...

5. ## check out this link

6. no-one >>> 00011111 == 31

I'm also very new and from seeing that its multiplied by two, why isn't 00011111 =="30" instead of 31?, therefore making it an even number and also making a multiple of 16.....am I s just stupid?, or don't I understand binary maths?

7. Also, what are the types of number maths that C++ is based on?

8. huh?

9. I'll see if I can make this easier ->

128 64 32 16 8 4 2 1
---------------------------------------
0 0 0 0 0 0 0 1 = 1
0 0 0 0 0 0 1 0 = 2
0 0 0 0 0 0 1 1 = 3
0 0 0 1 1 1 1 1 = 31
1 1 1 1 1 1 1 1 = 255

If a '1' appears in a column then the number at the top of the column is added to the final number.

PS After viewing this page i realise that the chart may not come out that well, oh well

10. see if this helps. paste code in and run

#include "stdafx.h"
#include <iostream.h> // add library file
#include <iomanip.h> // add library file

int main(int argc, char* argv[])
{
short number; // declare variables

short firstPart; // declare variables

short secondPart; // declare variables

int binary1,binary2,binary3,binary4; // declare variables

int remain1; // declare variables

int remain2; // declare variables

int remain3; // declare variables

cout<<"Enter an integer between 0 and 15"<< endl;
cin>>number; // user input

firstPart=number/10;
secondPart=number%10;

binary1 = number / 8; // formula for binary number conversion
remain1 = number % 8; // formula for binary number conversion
binary2 = remain1 / 4; // formula for binary number conversion
remain2 = remain1 % 4; // formula for binary number conversion
binary3 = remain2 / 2; // formula for binary number conversion
remain3 = remain2 % 2; // formula for binary number conversion
binary4 = remain3; // formula for binary number conversion

cout<<"N1="<<number<<"\tB1="<<binary1<<binary2<<bi nary3<<binary4;
cout<<"\tThe digits are \t"<<" "<< firstPart<<" "<<setw(3)<< secondPart << endl;

return 0;
}

11. ## may be useful

It may be useful !!

#include <iostream>

using namespace std;

void main()
{

char BinaryStr[32]={0};
unsigned long Result=0;

cout << "Input a binary number: ";
cin >> BinaryStr;

for(short i=strlen(BinaryStr);i>=1;i--)
{
if(BinaryStr[i]=='1')
Result |= 1; //bitwise 0|1 = 1
Result<<=1;
}
if(BinaryStr[i]=='1')
Result |= 1; //bitwise 0|1 = 1

cout << Result <<endl;
}

12. The last program is something wrong!
I think the the correct answer is this....
Using bitwise and shift operators can make your program
much faster...

#include <iostream>

using namespace std;

void main()
{

char BinaryStr[32]={0};
unsigned long Result=0;

cout << "Input a binary number: ";
cin >> BinaryStr;

for(short i=0;i<strlen(BinaryStr)-1;i++)
{
if(BinaryStr[i]=='1')
Result |= 1; //bitwise 0|1 = 1
Result<<=1;
}
if(BinaryStr[i]=='1')
Result |= 1; //bitwise 0|1 = 1
cout << Result <<endl;
}

13. UneducatedOne,

>I'm also very new and from seeing that its multiplied by two, why isn't 00011111 =="30" instead of 31?,

sorry, i didn't explain that well. see phantoms chart at the end of this post.
00000001 is 1 so the first number is 1 and it proceeds in multiples of two from then on. so if the first bit(not getting into bit ordering) is 1 the number is odd. hence 31 and not 30.

>am I s just stupid?, or don't I understand binary maths?

actually no to both, i just didn't explain it well.

>Also, what are the types of number maths that C++ is based on?

I would say decimal, all the math in computers is done at the lowest level in binary but it can easily be made to represent decimal math which is usually the case, so knowing binary math is not neccessary to use the math facilities of C++ or virtually any language for that matter.

Xterria,

questions? spit them out.

phantom,

code tags should help with the chart.

Code:
```128 64 32 16 8 4 2 1
---------------------------------------
0  0  0  0 0 0 0 1 = 1
0  0  0  0 0 0 1 0 = 2
0  0  0  0 0 0 1 1 = 3
0  0  0  1 1 1 1 1 = 31
1  1  1  1 1 1 1 1 = 255```

14. >phantom,

>code tags should help with the chart.

I admit I make a lousy teacher I have no problem explaining binary to a person one-to-one with a pencil and paper but I just don't know how to write it down for a third person

15. nah, you did better than me...