Conversion between base 10 and base 2

Discussion in 'Computer Science & Culture' started by cfso1952, Nov 7, 2004.

Thread Status:
Not open for further replies.
  1. cfso1952 Registered Member

    Messages:
    7
    How to convert from base 2 to 10 or 10 to 2????????
    Including conversion of integers, decimals

    How to carry out mathematical operations in base 2????????????????
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. geodesic "The truth shall make ye fret" Registered Senior Member

    Messages:
    1,002
    To convert from base 10 to base 2:
    find out how many digits you need by taking log base 2 of your number, which we shall call x, rounded down plus 1.
    Take n equal to the number of digits.
    Set each digit (starting from the left) by doing integer division of number x, and then set x as the remainder.
    decrement n and repeat the previous step until n=0.
    eg. 168 has 8 digits, 10101000

    To go from 2 to 10:
    Multiply each digit by 2 to the power of of its distance from the right minus 1.
    eg. 1001101=1x1+1x4+1x8+1x64=77.

    Why do you ask?
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. Voodoo Child Registered Senior Member

    Messages:
    1,296
    decimals depends upon the format of the number. As I recall the number is normalised in the form of .101110101(there is an implicit 1 at the start that need not be recorded). The exponent is used to tell how far to move the "decimal" point. There is also a sign bit.

    The IEEE convention is:

    1 bit sign /8 bit exponent/23 bit mantissa:

    0 11111111 0101010 10101010 10101010

    the exponent(unsigned) is converted to decimal, and then has 127 subtracted from it. if -ve, the dp goes <---. If +ve the dp goes --->.

    So the mantissa, 11111111. (or 2<sup>7</sup> after the bias is removed) means the dp goes 7 to the right. That is your floating point.

    To convert a binary no to base human:
    eg.
    101010.101

    is merely 2<sup>5</sup> * 1 +
    2<sup>4</sup> *0+
    2<sup>3</sup> *1 +
    2<sup>2</sup> *0 +
    2<sup>1</sup> *1 +
    2<sup>0</sup> *0 +
    2<sup>-1</sup> *1 +
    2<sup>-2</sup> *0 +
    2<sup>-3</sup> *1


    does that make any sense at all?
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. geodesic "The truth shall make ye fret" Registered Senior Member

    Messages:
    1,002
    Surely the exponent is the power of two to which the number is raised? Your exponent (which you have mis-named the mantissa) is 128 in decimal, which means the dp. moves 128 places right. You're multiplying by the exponent. And isn't the exponent in twos complement form, where the msb is negative, and all the others are positive? That would make your exponent -1.
     
  8. Voodoo Child Registered Senior Member

    Messages:
    1,296
    True. Should read: "(or 2<sup>7</sup> after the bias is removed) means the dp goes 2<sup>7</sup> to the right. ". Which is also wrong(should be 2<sup>7</sup>-1)

    No, it's biased. The reason is that 2's comp would make comparisons too hard.

    from http://www.math.grin.edu/~stone/courses/fundamentals/IEEE-reals.html

    edit: was also incorrect in that the bias is added and not subtracted.
     
    Last edited: Nov 8, 2004
Thread Status:
Not open for further replies.

Share This Page