Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MIPS Decimal to binary

  1. Oct 19, 2011 #1
    Hello. I was simply wondering if anyone has any idea on how to write a MIPS code to convert a decimal number to its binary number. Anyone can help me?
  2. jcsd
  3. Oct 19, 2011 #2


    User Avatar
    Science Advisor

    What is the memory representation of your number?
  4. Oct 19, 2011 #3
  5. Oct 19, 2011 #4


    User Avatar
    Science Advisor

    If its a standard word based integer, it is already in binary form in memory, and all you have to do is to get each bit and print the value. I don't know the platform you are using so I'll do pseudo-code:

    Assuming unsigned integer:

    x = size of word in bits
    For i = 0 to x-1
    bitarray[x-i-1] = (Word Value >> i) AND 1
    Next i

    The >> is a right shift operator, and AND is a bitwise AND instruction, not the comparison operator.

    Bitarray has an array containing each digit value from left to right as you would read it if it were printed. If you want it the other way just modify the index in the loop. What you would do instead of creating the array, you would basically allocate some space for a string and then the value of the jth character would be equal to:

    string[j] = '0' + (((Word Value >> i) AND 1) AND 255).
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook