Binary

I need to convert integer into a binary form, so far I have this code which convert it , but in a reverse order and I cannot reverse it in a right order . PS you cannot use array or any others data structures which is frustrating , you can also use while , do while or for loop. Sorry for my bad English in advance

1
2
3
4
5
6
7
int number=12;
	int newNumber=0;
	while (number!=0){
		int remainder=number%2;
		newNumber=number/2;
		number=newNumber;
		std::cout<<remainder<<" ";
Maybe this one will help you?


int remainder, digits = 0, dividend = palindrome;
while(dividend != 0)
{
dividend = dividend / 2;
digits++;
}
int array[digits];


Whats is palindrome?
@ jasongog24: if you can't/won't use code tags for your own code, then it means that you shouldn't try (or pretend) to help beginners.

@ Scruffy: you could use recursion to solve this without using data structures.

Recursion is the technique by which a function calls itself.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

void print_base2(int n)
{
    if (n < 2)
        std::cout << '\n' << n;
    else
    {
        print_base2(n / 2);
        std::cout << n % 2;
    }
}

int main()
{
    print_base2(12);
    print_base2(123);
    print_base2(382);
    print_base2(200);
    std::cout << std::endl;
}

Last edited on
Well the funny think is that we did not cover functions yet and he does not want us to use it. He only allows us to use decisions such us if and else and loops.Well is that possible to do with a third variable
Its really frustrated when you cannot use arrays or if I believe Hash table will be also useful
Hash table? I do not know how that would be useful in this case.

Anyway, if you want to "cheat" you could just use the std::bitset container.
You could also use the std::hex manipulator with a hex-to-bin conversion table.

http://www.cplusplus.com/reference/bitset/bitset/
http://www.cplusplus.com/reference/ios/hex/

 hex    bin
-------------
  0    0000
  1    0001
  2    0010
  3    0011
  4    0100
  5    0101
  6    0110
  7    0111
  8    1000
  9    1001
  A    1010
  B    1011
  C    1100
  D    1101
  E    1110
  F    1111


For example, the value 27110 would become 10F16 which according to the table above would be:

10F16 == 0001000011112
Last edited on
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
#include <iostream>

int main()
{
    for( unsigned int decimal : { 0, 12, 32, 49, 124, 268, 532, 1028, 2197, 5238 } )
    {
        std::cout << decimal << ' ' ;

        unsigned long long binary = 0 ;
        unsigned int power = 0 ;

        while( decimal > 0 )
        {
            unsigned long long bit = decimal % 2 ;

            for( unsigned int i = 0 ; i < power ; ++i ) bit *= 10 ;
            binary += bit ;

            decimal /= 2 ;
            ++power ;
        }

        std::cout << binary << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/2f775b888948d68e
That was really stupid; the inner for-loop is a waste of time.

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>

int main()
{

    for( unsigned int decimal : { 0, 12, 32, 49, 124, 268, 532, 1028, 2197, 5238 } )
    {
        std::cout << decimal << ' ' ;

        unsigned long long binary = 0 ;
        unsigned long long multiplier = 1 ;

        while( decimal > 0 )
        {
            unsigned long long bit = decimal % 2 ;
            binary += bit * multiplier ;

            decimal /= 2 ;
            multiplier *= 10 ;
        }

        std::cout << binary << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/df1df9dda23a59a3
Topic archived. No new replies allowed.