Converting an Integer to Binary

Mar 29, 2012 at 5:29am
I am writing a program that converts any integer into binary. I have provided a working program that outputs the values of an array that stores the necessary ones or zeroes. The problem arises when I want to convert this array into a single entity so I can just say "cout << entity << endl;" instead of saying "cout << array[?] << endl;" in a loop. Also, how efficient is my algorithm and how can it be improved?
Thanks!
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
35
36
#include <iostream>
#include <iomanip>

using namespace std;

int convert(int palindrome)
{
    int remainder, digits = 0, dividend = palindrome;
    while(dividend != 0)
    {
        dividend = dividend / 2;
        digits++;
    }
    int array[digits];
    dividend = palindrome;
    for(int i = digits - 1; i >= 0; i--)
    {
        remainder = dividend % 2;
        array[i] = remainder;
        dividend = dividend / 2;
    }
    cout << setw(3) << palindrome << " in binary is" << setw(3);
    for(int j = 0; j < digits; j++)
    {
        cout << array[j];
    }
    if(palindrome == 0)
    cout << 0;
    cout << endl;
}

int main()
{
    for(int y = 0; y < 128; y++)
    convert(y);
}
Last edited on Mar 29, 2012 at 5:42am
Mar 29, 2012 at 6:41am
Hello. here is a good and efficient algorithm.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

void Convert(unsigned int val)
{
   unsigned int mask = 1 << (sizeof(int) * 8 - 1);

   for(int i = 0; i < sizeof(int) * 8; i++)
   {
      if( (val & mask) == 0 )
         cout << '0' ;
      else
         cout << '1' ;

      mask  >>= 1;
   }
   cout << endl ;
}

int main()
{
   for(unsigned int i = 0; i < 128; i++)
      Convert(i);
}
Last edited on Mar 29, 2012 at 6:41am
Mar 29, 2012 at 3:29pm
Being new at programming, I am intrigued by your method and would like to know how it works. On a different note, what makes it more efficient? I tested the time it took for each algorithm to print out the binary equivalents of 0 to 1023 and my algorithm was consistently faster by about 5 seconds. Also, I still need an idea on how I would convert the string of 1's and 0's into a single usable entity.
Mar 29, 2012 at 6:31pm
1
2
3
4
5
6
7
    int remainder, digits = 0, dividend = palindrome;
    while(dividend != 0)
    {
        dividend = dividend / 2;
        digits++;
    }
    int array[digits];


There are no variable length arrays in C++. You probably have a compiler extension enabled which allows this, but it isn't legal C++.

As for the speed, the time spent on the calculation is overwhelmed by the time spent on output, and his version generates quite a bit more output than yours.

Last edited on Mar 29, 2012 at 7:00pm
Mar 29, 2012 at 9:19pm
There is no 'conversion' here. Just building a string that corresponds to the (already binary) representation of the number.

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
35
36
#include <iostream>
#include <limits>
#include <string>

using namespace std;

string toBinStr(unsigned int val)
{
	// mask has only the leftmost bit set.
	unsigned int mask = 1u << (std::numeric_limits<unsigned>::digits-1) ;

	// skip leading bits that are not set.
	while ( 0 == (val & mask) && mask != 0 )
		mask >>= 1 ; // shift all bits to the right by 1

	string binStr ;
	binStr.reserve(std::numeric_limits<unsigned>::digits+1) ;

	do
	{
		// add a '1' or '0' depending the current bit.
		binStr += static_cast<char>(val & mask) + '0' ;

	} while ( (mask>>=1) != 0 ) ; // next bit, when mask is 0 we've processed all bits

	return binStr ;
}

int main()
{
   for(unsigned int i = 0; i < 128; i++)
   {
      string entity = toBinStr(i) ;
      cout << entity << '\n' ;
   }
}


google bitwise operators if you don't know what's going on here.
Last edited on Mar 29, 2012 at 9:33pm
Topic archived. No new replies allowed.