binary results are reversed

Mar 9, 2010 at 2:21pm
closed account (STR9GNh0)
hi, i am trying to perform a binary conversion when i am stuck at a point where from Decimal i convert it to Binary the results will be in reversed. Is there a way to reverse the result to make it right?

my code is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int main ()
{
   int n = 0, number = 0;
 
   cout << "Enter a decimal number";
   cin >> number >> n;

int a;

if (number > 0)
{
   cout << "Binary representation: ";

   while (number > 0)
   {
      a = number % 2;	   	   	   	   
      number = number / 2;
      cout << a;
   }

   cout << endl;
}

return 0;
}


but my results eg: decimal i key in as: 589
results came out as: 1011001001 (its in reverse)
answer should be: 1001001101 instead.

i know by using a counter i could reverse it by recalculate the length of the result but i have no idea on how to do it and where to place it.

any help would be appreciated.
Last edited on Mar 9, 2010 at 2:25pm
Mar 9, 2010 at 2:58pm
build a std::string with the result instead of cout'ing it directly in the while loop.
The std::string will then be reversed.

Use std::reverse() to reverse it so it is in the correct order.

Mar 9, 2010 at 3:00pm
closed account (STR9GNh0)
i cant use reverse. is there any other method?

oh and i forgot to add-in, string isn't allowed here as well.
Last edited on Mar 9, 2010 at 3:01pm
Mar 9, 2010 at 3:07pm
Then you'll just have to keep the results in an array and then read the array backwards for printing.
Mar 9, 2010 at 3:13pm
closed account (STR9GNh0)
thanks for the help but how?

2. no alternative method such as using ++ to do it?
Mar 9, 2010 at 3:26pm
The only other way would be to extract the bits from the most significant end, instead of the least significant end like you're doing, but that's not easier.
Mar 9, 2010 at 3:32pm
closed account (STR9GNh0)
how should i go about??
Mar 9, 2010 at 6:11pm
i & ( 1 << N )

gets you the Nth bit of i (0 or 1) where the LSB is bit #0.
Mar 13, 2010 at 5:24am
I think the best way would be to completely redo the way you go about it, finding the left-most digits first and outputting them in order. The following code will do it (although I'm guessing there is a more efficient way to write it):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
#include<string>
#include<math.h>
using namespace std;

int main() //Start of main function
{

    //Variables
    int number,n=0,a,c;

    cout<<"Enter number to be converted to binary: ";
    cin>>number;//number to be converted to binary
    a=number; //placeholder for number variable
    n--;
    do 
    {
        n++;
        number=number/2;  //figure out value for n
    }while(number>0);
    number=a; //revert number back to original input
    do
    {
       c=pow(2,n); //c=2^n
       a=number/c;
       n--;
       cout<<a;
       number%=c;
    }while(n>=0);



    return 0;
}

Mar 13, 2010 at 5:53am
There's a much better way to get 2n than using pow(). Namely, what jsmith suggested.
1<<n == 2n
Topic archived. No new replies allowed.