Decimal to Binary

Hi,,,


I want to save the following answers in one integer value.
how can i do that ?


PS:I wrote this function to convert from decimal to binary.
I need to use the binary number later on.

For example assume dec=13
i want to save(1101) in one integer value

Thanx

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

int main()
{
	int dec;
	
	cout<<"Please insert decimal number: ";
	cin>>dec;

	while(dec!=0)
	{
		cout<<dec%2;
		dec/=2;
	}

	cout<<endl;
	system("pause");

	return 0;
}
i want to save(1101) in one integer value

I would advise against using an integer to store an apparent binary representation of a number. For one thing, the number of digits available is very limited, for another, it can be very confusing, as the value looks like an ordinary integer, but you won't be able to do any of the typical arithmetic operations using that value.

A string would be better. But really it depends what it is you are attempting to achieve.
I need to use the binary number later on.
How will it be used?
Last edited on
This concept is from my subnet calculator (posted in articles.) Using bitwise operators you can make short work of the problem. Assign the customary 128 bits to a variable to indicate the binary mask and use the bitwise AND and SHIFT operators, Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int decimal = 0;
	vector<int> binary;
	cout << "Enter Decimal Number: ";
	cin >> decimal;
        int mask = 128;
        while (mask)
        {
           binary.push_back((decimal & mask) != 0);
			cout << ((decimal & mask) != 0);
            mask >>= 1;
        }
	cout << "  : Binary" << endl;
	return 0;
}


P.S. - Chervil is the man that taught me about this :)
Last edited on
I will use it to

convert from binary to octal
convert from binary to hex

the program that i work on it is to change from any base to (HEXADECIMAL,OCTAL,BINARY,DECIMAL)

I already done converting from binary to decimal using an integer value
When we convert between bases by hand, using a pencil and paper, it is very convenient to convert from binary to either octal or hexadecimal, simply by considering groups of three or four bits at a time.

But when using a computer, rather than being simpler that way, it would just generate extra work and complexity. I'd suggest just convert direct from a plain int directly to whichever base is required.

That's my opinion anyway.
this is my method of converting small binary numbers to decimals:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    int x, t=0, digit, binary;
    vector<int> digitstack;
    cin >> x;
    while(x>0)
    {
        digit = x % 10;
        x = x / 10;
        digitstack.push_back(digit);
    }


        while(t!=digitstack.size())
    {
        binary=binary + digitstack[t] * pow((double)2 , (double)t); t++;

    }

    cout << binary;



What do you think? Also, using same method you can convert from decimal to binary: you push 1's and 0's to the container and then just multiply them by 10^t.
Also sorry, binary should be named x here and x should be named binary.
Last edited on
Thank you rcast for help

but I didn't understand what did you do in the function,,, and i have to explain how i wrote my code to the teacher. So i need to write it in simple way :)


Chervil,,,
How can i use string to save the values ?
Topic archived. No new replies allowed.