Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing integer to binary string of digits

I'm currently working on a simulation of the MIPS processor in C++ for a comp architecture class and having some problems converting from decimal numbers to binary (signed numbers both ways). Everything's working fine until the very last bit because my current algorithm falls into out of bounds areas for int on 1<<=31. Just need a nudge in the right direction to get it up and running. Thanks!

//Assume 32 bit decimal number string DecimalToBinaryString(int a) {     string binary = "";     int mask = 1;     for(int i = 0; i < 31; i++)     {         if((mask&a) >= 1)             binary = "1"+binary;         else             binary = "0"+binary;         mask<<=1;     }     cout<<binary<<endl;     return binary; } 

I'm also including my other algorithm for completeness. I apologize for the lack of comments, but it's fairly straight forward.

int BinaryStringToDecimal(string a) {     int num = 0;     bool neg = false;     if(a.at(0) == '1')     {         neg = true;         for(int x = a.length()-1; x >= 0; x--)         {             if(a.at(x) == '1')                 a.at(x) = '0';             else a.at(x) = '1';         }         a.at(a.length()-1) += 1;         for(int x = a.length()-1; x >= 0; x--)         {             if(a.at(x) == '2')             {                 if(x-1 >= 0)                 {                     if(a.at(x-1) == '1')                         a.at(x-1) = '2';                     if(a.at(x-1) == '0')                         a.at(x-1) = '1';                     a.at(x) = '0';                 }             }             else if(a.at(x) == '3')             {                 if(x-1 >= 0)                     a.at(x-1) += '2';                 a.at(x) = '1';             }         }         if(a.at(0) == '2')             a.at(0) = '0';         else if(a.at(0) == '3')             a.at(0) = '1';     }     for(int x = a.length()-1; x >= 0; x--)     {         if(a.at(x) == '1')             num += pow(2.0, a.length()-x-1);     }     if(neg)         num = num*-1;        return num;  } 

Also if anyone knows any good ways to go about writing these more efficiently I'd love to hear it. I've only had the two introductory programming classes but have been playing with different techniques to see how well I like their style.

like image 227
Paul Ruiz Avatar asked Nov 22 '11 04:11

Paul Ruiz


People also ask

How do you convert a string to a binary number?

To convert a string to binary, we first append the string's individual ASCII values to a list ( l ) using the ord(_string) function. This function gives the ASCII value of the string (i.e., ord(H) = 72 , ord(e) = 101). Then, from the list of ASCII values we can convert them to binary using bin(_integer) .

How do you convert an integer to a binary string in Python?

To convert int to binary in Python, use the bin() method. The bin() is a built-in Python method that converts a decimal to a binary data type. The bin() function accepts a number as an argument and returns its equivalent binary string prefixed with “0b”.

Which Java method is used to convert an integer into a binary string?

Java's Integer class has a method named toBinaryString to convert an integer into its binary equivalent string.

What is binary string in C++?

Special Binary String in C++ We have to find the lexicographically largest resulting string possible, at the end of any number of moves. So, if the input is like 11011000, then the output will be 11100100, this is because: The substrings "10" and "1100" are swapped.


2 Answers

There are actually standard one-liners for these.

#include <bitset>  std::string s = std::bitset< 64 >( 12345 ).to_string(); // string conversion  std::cout << std::bitset< 64 >( 54321 ) << ' '; // direct output  std::bitset< 64 > input; std::cin >> input; unsigned long ul = input.to_ulong(); 

See this run as a demo.

like image 161
Potatoswatter Avatar answered Oct 04 '22 09:10

Potatoswatter


Replace:

if((mask&a) >= 1) 

with either:

if ((mask & a) != 0) 

or:

if (mask & a) 

Your problem is that the last bit gives you a negative number, not a positive one.

like image 34
Jonathan Leffler Avatar answered Oct 04 '22 10:10

Jonathan Leffler