# Thread: Binary Number to Decimal

1. ## Binary Number to Decimal

Hello all.

I am new to programming and taking my first programming course which is basic C++ programming. However I am stuck on a exercise and not sure what to do.

The exercise is: Write a program that asks the user to input a 4 digit binary number and print its decimal equivalent.

Like I said this is a basic course and only a month in, so still new to things right now.

Right now I have

Code:
``` #include<iostream>

using namespace std;

int main()

{
int binary, decimal;

cout << "Enter a 4-digit binary number: " ;
cin >> binary;

decimal =

cout << "The decimal equivalent of " << binary << " is " << decimal << endl;

system("pause");
return 0;

}```
I am not sure what to do to make it so it would convert it to decimal. I know how to convert binary to decimal (To convert 1101 it is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8, or 1 + 0 + 4 + 8 or 13).

If someone could give me some help on what my calculation would be to convert it, that would be great.

2. That way, you read the number as decimal. You have to input the number as a string and use strtol().

3. Try preventing pausing the system because it only works on certain operating systems. Try 'cin.get()' instead if you want a user to be able to view the output.

4. I just need to know how to change what the user puts as binary to decimal. I know how to calculate binary to decimal but I only know that when I know what the binary number is, and the user can put any binary number.

The reason for system("pause"); is because I use Dev C++ and you need it to hold the exe window open.

5. Just loop through each bit of the int:
Code:
```for(int i = 0;i < sizeof(binary) * 8;++i)
{
int singleBit = (binary >> i) & 1;
// Do your sum calcuation here
}```

6. You explained it yourself when you asked the question, I think.
(To convert 1101 it is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8, or 1 + 0 + 4 + 8 or 13)
So the other thing you would have to do prior is break up a number like 1101.

You can find out the value of every place.

1101 % 10000 / 1000 = 1
1101 % 1000 / 100 = 1
1101 % 100 / 10 = 0
1101 % 10 = 1

7. Originally Posted by whiteflags
You explained it yourself when you asked the question, I think.

So the other thing you would have to do prior is break up a number like 1101.

You can find out the value of every place.

1101 % 10000 / 1000 = 1
1101 % 1000 / 100 = 1
1101 % 100 / 10 = 0
1101 % 10 = 1
Well my program has to let the user enter whatever 4 digit binary number he wants. So how do I calculate the binary number the user chocies, and convert it to decimal?

8. It works for any binary number up to 4 digits you can think of, as well as others, if you bothered to compute the other place values. I'd prove it to you, but at this point you know everything you need to, so do it and see for yourself.

9. Originally Posted by whiteflags
It works for any binary number up to 4 digits you can think of, as well as others, if you bothered to compute the other place values. I'd prove it to you, but at this point you know everything you need to, so do it and see for yourself.
I think I'm getting myself more confused. How do I take those numbers and actually make it work?

I've been on this all night and haven't gotten anywhere.

Here is the code I have and the problem with it.
Code:
```#include<iostream>

using namespace std;

int main()

{
int binary1, binary2, binary3, binary4, decimal1, decimal2, decimal3, decimal4, total;

cout << "Enter a 4-digit binary number: " ;
cin >> binary1 >> binary2 >> binary3 >> binary4;

decimal1 = binary4 * 1;
decimal2 = binary3 * 2;
decimal3 = binary2 * 4;
decimal4 = binary1 * 8;
total = decimal1 + decimal2 + decimal3 + decimal4;

cout << "The decimal equivalent of " << binary4 << binary3 << binary2 << binary1 << " is " << total << endl;

return 0;
}```
So in this code I wrote it will convert any binary number to decimal, however the problem is the user has to put the binary number in 1 number at a time, with either a space or enter.

I don't know how to do the calculation with just one statement. So the user could just enter the number as one number, instead of 4.

10. Read the binary number in as a string and then loop through the characters.

11. That bit of math I showed you computes place values.
Originally Posted by whiteflags
1101 % 10000 / 1000 = 1
1101 % 1000 / 100 = 1
1101 % 100 / 10 = 0
1101 % 10 = 1
All you really have to do is plug in those math expressions, on the left, into the formula you already know, which is
(To convert 1101 it is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8, or 1 + 0 + 4 + 8 or 13)
and the 1101, used as an example by both of us, is really an integer named binary.

12. I would read it into a string and process that. As you inspect each character you first multiply the running total by two and then add one to the running total if the char is a '1' (but not if it is a '0').
Then you just cout the running total.

13. You could just use bit operators:
Code:
```        string binary_string = "1010";
for(int i = 0; i < (int)real_binary_string.length(); i++){
ch = binary_string[i];
if(ch == '1'){
dec_number = dec_number | (int)pow((double)2, i);
}
}
cout << dec_number << endl;```