Trying to go from decimal to binary

Discussion in 'Programmer's Corner' started by nanobyte, Jun 6, 2010.

  1. nanobyte

    Thread Starter Senior Member

    May 26, 2004
    118
    1
    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?

    Code ( (Unknown Language)):
    1.    
    2.     int x;
    3.     int i;
    4.     int tobinary[1];
    5.  
    6.     cin >> x;
    7.  
    8.     while(x/2 > 0)
    9.     {
    10.      tobinary[i] = x % 2;
    11.      tobinary[i]++;
    12.     }
    13.  
    14.     cout << tobinary[0] << endl;  //displaying 1 bit
    15. [/i][/i]
     
  2. coldpenguin

    Active Member

    Apr 18, 2010
    165
    9
    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:
    Code ( (Unknown Language)):
    1.  
    2.     int x;
    3.     int i=0;
    4.     cout "Please enter an integer";
    5.     cin >> x;
    6.  
    7. // Not sure about this
    8. int *output=new int[int(sqrt(x))+1]; //Think this would do
    9.  
    10.     while((x/2) > 1)
    11.     {
    12.      output[i] = x % 2;
    13.      x/=2;  //this depends on your c implementation I think
    14.      i++;
    15.     }
    16.     while(i>0){
    17.     cout output[i];
    18.     i--;  //MSB
    19.     }
    20. delete(output);
    21. [/i][/i]
     
  3. nanobyte

    Thread Starter Senior Member

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

    Code ( (Unknown Language)):
    1.    
    2.     int x;
    3.     int i=1;
    4.     int tobinary[i];
    5.  
    6.     cout << "Enter an integer" << endl;
    7.     cin >> x;
    8.  
    9.     while((x/2) > 0)
    10.     {
    11.      tobinary[i] = x % 2;
    12.      x/=2;
    13.      i++;
    14.     }
    15.  
    16.     cout << "The binary equivlanet = " << endl;
    17.  
    18.     while(i>0)
    19.     {
    20.      cout << tobinary[i] << endl;
    21.      i--;
    22.     }[/i][/i][/i]
     
  4. AsmCoder8088

    New Member

    Apr 17, 2010
    15
    1
    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:

    Code ( (Unknown Language)):
    1.  
    2. #include <iostream>
    3.  
    4. using namespace std;
    5.  
    6. int main()
    7. {
    8.     int x;
    9.     unsigned int curMask;
    10.  
    11.     curMask = 0x80000000;
    12.     cin >> x;
    13.  
    14.     while (true)
    15.     {
    16.         if (x & curMask)
    17.         {
    18.             cout << 1;
    19.         }
    20.         else
    21.         {
    22.             cout << 0;
    23.         }
    24.  
    25.         curMask /= 2;
    26.  
    27.         if (curMask == 0)
    28.         {
    29.             cout << endl;
    30.             break;
    31.         }
    32.     }
    33.    
    34.     return 0;
    35. }
    36.  
    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:

    Code ( (Unknown Language)):
    1.  
    2. #include <iostream>
    3.  
    4. using namespace std;
    5.  
    6. int main()
    7. {
    8.     int x;
    9.     cin >> x;
    10.  
    11.     while (x >= 0)
    12.     {
    13.         if (x & 1)
    14.         {
    15.             cout << 1;
    16.         }
    17.         else
    18.         {
    19.             cout << 0;
    20.         }
    21.  
    22.         x = x >> 1;
    23.     }
    24.    
    25.     return 0;
    26. }
    27.  
    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).
     
  5. Harrington

    New Member

    Dec 19, 2009
    86
    3
    Very nice explanation Like the method as well
     
Loading...