Trying to go from decimal to binary

Thread Starter

nanobyte

Joined May 26, 2004
120
I'm currently refreshing myself with c++. I'm trying to write a program that converts a decimal input to a binary equivalent using an array. For some reason when the display console appears it doesn't display anything. Any suggestions?

Rich (BB code):
    int x;
    int i;
    int tobinary[1];

    cin >> x;

    while(x/2 > 0)
    {
     tobinary = x % 2;
     tobinary++;
    }

    cout << tobinary[0] << endl;  //displaying 1 bit
 

coldpenguin

Joined Apr 18, 2010
165
Have you tried typing?
The first call you are doing is a cin, why won't display anything, and will wait for a return character.
Also, you need to set i to be 0. You are using LSB first. Unless you are either entering 0, you will overflow your buffer.
In c++ I would think that the following would be closer:
Rich (BB code):
    int x;
    int i=0;
    cout "Please enter an integer";
    cin >> x;

// Not sure about this
int *output=new int[int(sqrt(x))+1]; //Think this would do

    while((x/2) > 1)
    {
     output = x % 2;
     x/=2;  //this depends on your c implementation I think
     i++;
    }
    while(i>0){
    cout output;
    i--;  //MSB
    }
delete(output);
 

Thread Starter

nanobyte

Joined May 26, 2004
120
I've made some progress. For some reason though the MSB is showing up as some other weird number.

Rich (BB code):
    int x;
    int i=1;
    int tobinary;

    cout << "Enter an integer" << endl;
    cin >> x;

    while((x/2) > 0)
    {
     tobinary = x % 2;
     x/=2;
     i++;
    }

    cout << "The binary equivlanet = " << endl;

    while(i>0)
    {
     cout << tobinary << endl;
     i--;
    }
 

AsmCoder8088

Joined Apr 17, 2010
15
Okay, so there's a couple of things wrong with that code.

First off, you can't resize the "tobinary" array once it is declared, at least not the way you're doing it. Only way to resize an array is to allocate memory, and retrieve a pointer to that memory block, and then afterwards you can mess with it.

But even so, use of the modulus operator is not quite right in this case. When you do "tobinary = x % 2", the value returned will be 0 when x is a multiple of 2, and 1 otherwise.

Supposing x were (decimal) 34 to begin with, you would expect the binary value to be (binary) 100010. But in this case, 34%2=0, then you are dividing x by 2, so you get 34/2=17, and 17%2=1, then 17/2=8, and 8%2=0, then 8/2=4, and 4%2=0, then 4/2=2, and 2%2=0, then your loop terminates because 1/2 evaluates to zero. So the result returned is 01000, which is not right.

There are many ways to resolve a binary string from a decimal number in C/C++, and here is one way:

Rich (BB code):
#include <iostream>

using namespace std;

int main()
{
    int x;
    unsigned int curMask;

    curMask = 0x80000000;
    cin >> x;

    while (true)
    {
        if (x & curMask)
        {
            cout << 1;
        }
        else
        {
            cout << 0;
        }

        curMask /= 2;

        if (curMask == 0)
        {
            cout << endl;
            break;
        }
    }
    
    return 0;
}
Essentially, you have an integer 'x', for which you want to output the binary representation. Obviously, it is stored as a binary number in your computer. But to actually display as a binary number is a bit more complicated in the C/C++ languages.

In the above code, I create a bit mask starting at the MSB for a 32-bit computer (2^31).

Then, you enter a loop to test the current bit mask on x. So we test to see if the 31st bit (zero-based index) is a 1 or 0, and output the result. Then we divide the bit mask by 2, allowing us to test the 30th bit. Continue until you get to the 0th bit; test that, and you are done.

With the above code, the output result has leading zeros, which you may not want. Either you can put in a flag to test if you have yet output a 1 (and if the flag is set, it is acceptable to output zeros), or you can use this code:

Rich (BB code):
#include <iostream>

using namespace std;

int main()
{
    int x;
    cin >> x;

    while (x >= 0)
    {
        if (x & 1)
        {
            cout << 1;
        }
        else
        {
            cout << 0;
        }

        x = x >> 1;
    }
    
    return 0;
}
The downside to this second example is that it only works for non-negative values of x, since a negative value in binary is represented as two's complement and you would want the first code example to do that (where you make use of the full 32 bits in the bit mask).
 
Top