Can anyone help me with a program that converts decimal number into binary number? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can anyone help me with a program that converts decimal number into binary number?

18th Jun 2016, 12:02 PM
Navishta Hashmi
Navishta Hashmi - avatar
17 Answers
+ 3
consider the following : #include <iostream> #include <bitset> int main() { int myDecimalNumber; std::cin >> myDecimalNumber; std::string binary = std::bitset<8>(myDecimalNumber).to_string(); //to binary std::cout<<binary<<"\n"; unsigned long decimal = std::bitset<8>(binary).to_ulong(); std::cout<<decimal<<"\n"; return 0; } this will take any decimal value and convert it to binary. but take caution here, since we set the bitset to contain 8 bits, that means the largest number we can use is 255 (2^8 - 1). once I figure out an appropriate way to place the correct number of bits I'll update. but nonetheless this should be a good starting place for you. enjoy, happy coding!
18th Jun 2016, 12:47 PM
destro
0
Thank you so much. But I'm new to coding. Can you now help explain it? Please. That std:: thing.
18th Jun 2016, 12:49 PM
Navishta Hashmi
Navishta Hashmi - avatar
0
of course. so std:: is a namespace. a namespace can be thought of as a way to ID a class. I'm c++ the most common class we use is the standard class. this contains our cins,couts, even our strings. the :: portion just separates the namespace and what were looking for. commonly what you'll see is people do this using namespace std; what that does is it tells the compiler "hey, when yu run up against something that you don't see, check it against the std namespace". you can use the using method to save yourself a lot of typing
18th Jun 2016, 12:59 PM
destro
0
so if we modify the code with using namespace std we get this: #include <iostream> #include <bitset> using namespace std; int main() { int myDecimalNumber; cin >> myDecimalNumber; string binary = bitset<8>(myDecimalNumber).to_string(); //to binary cout<<binary<<"\n"; unsigned long decimal = bitset<8>(binary).to_ulong(); cout<<decimal<<"\n"; return 0; }
18th Jun 2016, 1:00 PM
destro
0
Okay. I get it now. Thank you so much.
18th Jun 2016, 1:02 PM
Navishta Hashmi
Navishta Hashmi - avatar
0
Well, my language professor gave us these challenges. So, when I went up to him with this problem some days ago, he mailed me this-
18th Jun 2016, 1:04 PM
Navishta Hashmi
Navishta Hashmi - avatar
0
int dectobin(int dec, int power_of_two) { if(dec == 0) {    cout<<"0"; } else if(dec/(int)pow(2, power_of_two)) { int remainder = dectobin(dec, power_of_two+1); if(remainder/(int)pow(2, power_of_two)) { cout<<"1"; return remainder - (int)pow(2, power_of_two); } else { cout<<"0"; return remainder; } } else { return dec; } }
18th Jun 2016, 1:04 PM
Navishta Hashmi
Navishta Hashmi - avatar
0
Now, you're working on this, it might help you somehow. So, I sent you. 'Cz I'm not able to understand the logic.
18th Jun 2016, 1:05 PM
Navishta Hashmi
Navishta Hashmi - avatar
0
he's using a VERY round about way to get this done. honestly it's much slower and from glancing over it quickly it's error prone I believe, haven't tested I yet. I would stick with the bitset method, it will provide you with accurate results and will be much faster
18th Jun 2016, 1:25 PM
destro
0
True. Yours is lot easier. Although, I'm searching google for those string binary=..........to_string (); for I'm new to such coding. But I could understand your logic. It's just that I haven't used bitset before. It's simple unlike my Sir's.
18th Jun 2016, 1:29 PM
Navishta Hashmi
Navishta Hashmi - avatar
0
And do provide me. Thank you, again.
18th Jun 2016, 1:30 PM
Navishta Hashmi
Navishta Hashmi - avatar
0
what to_string(); does is it takes a value and converts it to a string so it's workable. in our case we want to be able to print the results out to the screen so we convert it to a string and it just becomes easier to print out. bitset is just one of those predefined functionalities of c++. The best advice I can give to someone learning any programing language is to research. if you come across something that you don't recognize or it doesn't quite make sense find a reference for it. stack overflow and msdn are great places to clarify things. I use those two sites all the time
18th Jun 2016, 1:35 PM
destro
0
I'll get to those sites. Yeah. Research. That's the only way we can learn something. Well, I'm clear with to_string (); now and so with to_ulong ();
18th Jun 2016, 1:40 PM
Navishta Hashmi
Navishta Hashmi - avatar
0
awesome, and I mistyped something. the idea of it being workable is the same but I glossed over an important part. with the code I gave you, we made the variable binary a string. we then set it equal to the bitset function. however the bitset function will return a different variable type, an unsigned long. so that's why we have to comvert it to_string and to_ulong, it just makes it the same variable type as what we told it it should be
18th Jun 2016, 1:46 PM
destro
0
Okay. I'm clear with it now. Thank you. I'll refer to you next time I get into some trouble. You explain very well.
18th Jun 2016, 2:02 PM
Navishta Hashmi
Navishta Hashmi - avatar
0
Suppose input decimal number is 13 Step 1. 13/2 , Remainder = 1, Quotient = 6 Step 2. 6/2 , Remainder = 0, Quotient = 3 Step 3. 3/2 , Remainder = 1, Quotient = 1 Step 4. 1/2 , Remainder = 1, Quotient = 0 Now, the Binary equivalent of 13 is the remainders in reverse order : 1101 Here is the C program to convert decimal ti number and vice versa http://www.techcrashcourse.com/2015/08/c-program-to-convert-decimal-number-binary.html http://www.techcrashcourse.com/2017/01/cpp-program-to-convert-binary-to-decimal.html
15th Apr 2017, 6:22 AM
Arun Kumar
Arun Kumar - avatar
- 1
Wonderful! Welcome to c++, I hope you enjoy your stay!
18th Jun 2016, 1:05 PM
destro