Changing integer to binary string of digits

后端 未结 5 1023
梦毁少年i
梦毁少年i 2020-12-15 04:24

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

5条回答
  •  不知归路
    2020-12-15 05:08

    I checked your code and couldn't find any error. Here is the code that i used...

    #include 
    #include 
    using namespace std;
    
    int main ()
    {
      int a=1111165117;
      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<

    Output will be 1001110001010110101111010011101. I you can run this on ideone

提交回复
热议问题