C # implements 10-in-Turn 2-binary

Source: Internet
Author: User



These days in the review of computer principles, see binary system suddenly think of binary system to 10 into the formula, and then second thought to 10 binary system to the formula seems to have no impression, that simply write it out.



The results of the slag I found, can not write it! What sequence, logarithm, xx function forget all clean, and need to have to judge the place, so collapsed, before the algebra problem did not write conditions Ah ~



Simply use C # code to get out (although in C # there are methods to directly convert)








Binary
value 10 binary values Formula
0 0 0
1 1 1
10 2 F (2) =10^1 =10
11 3 F (3): The smallest integer bit than log?3 1, recording 10^1, and 2^1 =1,f (1) = 1, the final result is 10^1+f (1) =10+1=11
100 4 F (4): log?4=2, Integer, direct return 10^2
101 5 F (5): The smallest integer bit than Log?5 2, recorded 10^2, and 2^2 =1,f (1) = 1, the final result is 10^2+f (1) =100+1=101
110 6 F (6): The smallest integer bit than Log?6 2, recording 10^2, and 6-(2^2) =2,f (2) = 10, the final result is 10^2+f (2) =100+10=110
111 7 ..................
1000 8 ..................
1001 9 ..................
1010 10 ..................
1011 11 ..................
1100 12 ..................
1101 13 ..................
1110 14 ..................
1111 15 F (15): The smallest integer bit than log?15 3, recording 10^3, and 15-(2^3) =7,f (7) = 111, the final result is 10^3+f (7) =1000+111=1111
10000 16 F (16): Smallest integer bit than log?6 4, integer, direct return 10^4


The formula is like this. If the binary of the 10 binary number n is asked, the logarithm log of N of the base 2 is first obtained. N, Judge log? n is an integer, if 10^log is returned directly for an integer? N, otherwise find out than log? n the smallest integer t (e.g. 1.001 takes 1, 2.02 takes 2, 3.9 takes 3). Find and record the 10^t, then subtract the n 2^t and repeat the above steps, and the returned value is added to the 10^t of the record.



Above we set this function to f (n);



Paste code, no doubt, with a recursive ~


  private double DecimalToBinarySystem(int decimalNumber)
        { double standard = 0; if (decimalNumber==0)
            { return 00;
            } if (decimalNumber==1)
            { return 01;
            } var doubleValue = (double) decimalNumber; var logarithm = Math.Log(doubleValue,2 ); var logFormat = Math.Truncate(logarithm); var baseNum = Math.Pow(10, logFormat); if (logarithm.Equals(logFormat))
            { return baseNum;
            } var leftNumber = doubleValue - Math.Pow(2, logFormat); return baseNum + DecimalToBinarySystem((int)leftNumber);


        }


only integers greater than or equal to 0 have been tested, and negative and fractional numbers are not tested.



Just be happy to think about it and welcome it.






C # implements 10-in-Turn 2-binary


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.