Click here to Skip to main content
15,887,428 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i am trying to convert binary to decimal by this code the problem is that how to use 2 raise to the power -

C++
#include<iostream>
using namespace std;
#include<conio.h>

int main()
{
	int a,b,temp=0,n;
	cout<<"Enter the range of natural no's : ";
	cin>>b;
	n==b;
	for(a=0;a<=n;a++)
	{
		temp=temp+n%10;
		temp=temp*2^a;
		n=n/10;
	}
	cout<<"\nThe sum of given natural no's is : "<<temp;
	getch();
	return 0;
}
Posted
Updated 28-Jun-15 16:24pm
v4
Comments
[no name] 26-Jun-15 22:23pm    
http://www.cplusplus.com/reference/cmath/pow/
Sergey Alexandrovich Kryukov 26-Jun-15 23:37pm    
No, no. Pay attention that the operands are integer. Please see my answer.
—SA
[no name] 27-Jun-15 6:04am    
No, no yourself. Pay attention to the question.
Sergey Alexandrovich Kryukov 27-Jun-15 8:04am    
Why? What part of the question? Do you mean "decimal to binary"? Still no floating-point arithmetic...
But I need to update the answer.
—SA
CPallini 27-Jun-15 4:47am    
What's exactly your requirement? It doesn't look a decimal to binary conversion.

Let's note that the operands are integers. The fastest way to find the integer power a of 2 is
C++
int power = 1 << a;


The method suggested by Wes Aday in his comment to the question is only good for floating point calculation; for integers it would involve double type cast — not good.

Please see: http://www.cprogramming.com/tutorial/bitwise_operators.html[^].

[EDIT]

But if the ultimate goal is to calculate the binary string representation, the solution is different. (Only not "decimal to binary": your integer values cannot be "decimal", only strings can be. You convert binary to binary strings, that is, extract bytes).

To find binary string, you need to extract each byte and fill in the string with 0 or 1. Here is how. First, create a bit mask, which should be a power of 2, see above. Do it in cycle for each bit. For a mask for each bit position, you have to find logical AND of the test number and the mask. The result is either zero or not. If zero, the text bit is clear, otherwise it is set:
C++
char bitDigit;
if ((1 << bit) & value) == 0)
    bitDigit = '0';
else
    bitDigit = '1';

Fill values of bitDigit left to right in the string, according to the bit position bit, in cycle.

That's all.

—SA
 
Share this answer
 
v3
Comments
nv3 27-Jun-15 1:19am    
My 5!
Sergey Alexandrovich Kryukov 27-Jun-15 1:20am    
Thank you.
—SA
nihal saxena 27-Jun-15 8:13am    
so where should i place "int power = 1 << a;" at initialisation or between the loop
Sergey Alexandrovich Kryukov 27-Jun-15 8:14am    
Please see my update to the question, after [EDIT].
—SA
Frankie-C 27-Jun-15 8:17am    
5ed.
Numbers could be represented as a polynomial of powers of the base.
I.e. 13 = 1x10^1 + 3x10^0
When the base is 2 as in binary representation by shifting 1 is as moving a coefficient=1 to the polynomial value equal to the exponent.
So 1<<a is equivalent to 1x2^a
In addition to Solution 1, watch the following problem:
C++
n==b;

Compares n and b and throws the result away! You probably meant to write:
C++
n = b;
 
Share this answer
 
Comments
[no name] 27-Jun-15 8:35am    
a 5 for the eagle eye :)
nv3 27-Jun-15 14:32pm    
Thanks!
If the question is, how to print the memory image of some integer value, you might use bitset. E.g.
C++
#include <bitset>
...
int x = ...
...
std::cout << "The binary value of x = " << x
          << " is " << std::bitset<sizeof(int)*8>(x)
          << std::endl;
See also http://www.cplusplus.com/reference/bitset/bitset/bitset/[^].
Cheers
Andi
 
Share this answer
 
Sure, 1st you want to add this to the top of your code:
C++
#include <cmath>


As for your power, there's a function for that:

C++
pow(5, 3);


Hope I helped you out some :)
 
Share this answer
 
v2
Comments
Afzaal Ahmad Zeeshan 27-Jun-15 13:38pm    
Perhaps you didn't read the question. He said he wants to convert decimal to binary.
Andreas Gieriet 28-Jun-15 7:05am    
I fixed the preformatted code blocks.
But as the comment above mentions, this is not a useful answer to the question.
Cheers
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900