How do I convert a very long binary number to decimal?

前端 未结 3 1513
走了就别回头了
走了就别回头了 2020-12-06 18:50

I have a binary number represented as 11.1111111 (the . being analogous to a decimal point). There are 2 bits before the point, and 1024 bits after the point. It was an exer

3条回答
  •  粉色の甜心
    2020-12-06 19:10

    Each binary digit after the decimal point represents the decimal weight of 2^-n, starting with n=1.

    This can be evaluated using any bignum library using Horner's method as: (this is just pseudocode)

    power_of_five = 1;
    digits = 0;
    while digits_left
      digits = digits * 10;
      power_of_five = power_of_five * 5;
      if (next_digit_is_set)
         digits = digits + power_of_five;
    end
    

    This will result a 1024 digit bignum, out of which only the first 309 are significant.

提交回复
热议问题