int to binary simplification??

Hello all, I wrote a program that converts a decimal (base 10) number to binary (base 2). It works perfectly fine, but I'm just wondering if there is a simpler way of doing 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
Write a program that reads in an integer and outputs the number in binary representation
*/

#include <iostream>
using namespace std;

//converts decimal to binary
void conversion(int decimal);

int main()
{
    cout<< "Please enter a positive integer: " << endl;
    int num;
    cin>> num;

    conversion(num);

return 0;
}

void conversion(int decimalInput)
{

    int decimal = decimalInput;         //changes input to decimal
    int bin1, bin2, bin3, bin4, bin5, bin6, bin7, bin8;     //each individual bit value
    cout<< "The Decimal number " << decimal << " converts to Binary number: ";
    //prints 8 bits
    if(decimal >= 16)
    {
        bin1 = (decimal >= 128) ? 1 : 0;
            if(bin1 == 1)                    //if the binary value is 0, then it does not subtract
                decimal -= 128;
        bin2 = (decimal >= 64) ? 1 : 0;
            if(bin2 == 1)
                decimal -= 64;
        bin3 = (decimal >= 32) ? 1 : 0;
            if(bin3 == 1)
                decimal -= 32;
        bin4 = (decimal >= 16) ? 1 : 0;
            if(bin4 == 1)
                decimal -= 16;
        bin5 = (decimal >= 8) ? 1 : 0;
            if(bin5 == 1)
                decimal -= 8;
        bin6 = (decimal >= 4) ? 1 : 0;
            if(bin6 == 1)
                decimal -= 4;
        bin7 = (decimal >= 2) ? 1 : 0;
            if(bin7 == 1)
                decimal -= 2;
        bin8 = (decimal >= 1) ? 1 : 0;
        //prints the binary code
        cout<< bin1 << bin2 << bin3 << bin4 << " "
            << bin5 << bin6 << bin7 << bin8 << endl;
    }
    //prints 4 bits
    else if(decimal >= 8 && decimal < 16)
    {
        bin5 = (decimal >= 8) ? 1 : 0;
            if(bin5 == 1)
                decimal -= 8;
        bin6 = (decimal >= 4) ? 1 : 0;
            if(bin6 == 1)
            decimal -= 4;
        bin7 = (decimal >= 2) ? 1 : 0;
            if(bin7 == 1)
                decimal -= 2;
        bin8 = (decimal >= 1) ? 1 : 0;
        cout<< bin5 << bin6 << bin7 << bin8 << endl;
    }
    //prints 3 bits
    else if(decimal >= 4 && decimal < 8)
    {
        bin6 = (decimal >= 4) ? 1 : 0;
            if(bin6 == 1)
            decimal -= 4;
        bin7 = (decimal >= 2) ? 1 : 0;
            if(bin7 == 1)
                decimal -= 2;
        bin8 = (decimal >= 1) ? 1 : 0;
        cout<< bin6 << bin7 << bin8 << endl;
    }
    //prints 2 bits
    else if(decimal >= 2 && decimal < 4)
    {
        bin7 = (decimal >= 2) ? 1 : 0;
            if(bin7 == 1)
                decimal -= 2;
        bin8 = (decimal >= 1) ? 1 : 0;
        cout<< bin7 << bin8 << endl;
    }
    //prints 1
    else if(decimal == 1)
    {
        cout<< 1;
    }
    //prints 0
    else if(decimal == 0)
    {
        cout<< 0;
    }
}
Yes, there is a much more elegant way of doing it. Try doing this on paper: write down any number. If it is even, write a 0 to the right. If it is odd, write a 1 to the side. Then, divide it by 2 and round down and write the result below the number. Keep repeating this process until you get to 0. Then, read the 0s and 1s on the right from bottom to top to get your binary number.

This works for converting to any base from any base - the numbers on the right are just the remainder of division.
Last edited on
There's also the "easy way" out, where you just use a std::bitset<sizeof(int)*CHAR_BIT> to store the integer and then just print that out (or use the to_string member function if you want it in string form).

Of course, that's not any fun, now, is it? :)
(In particular, it doesn't really teach you much about binary numbers and/or base conversions, so if that's what you were going for, then...)
Last edited on
Or you can use the bitwise operators.

1
2
3
4
5
x = foo & 1; // x=0 if low bit clear, x=1 if low bit set

foo >>= 1;  // shift bits 1 position to the right

// rinse, repeat 
Topic archived. No new replies allowed.