Decimal to binary algorithm

Source: Internet
Author: User
Tags binary to decimal decimal to binary

Here I have reorganized the decimal to binary algorithm and added the power algorithm, which is currently aimed at unsigned integers. Here I will keep updating the algorithm. The first is an unsigned integer: the code is as follows:

Package MASM. chapter1; </P> <p> public class decimaltobinary <br/>{< br/> Public static int length = 32; <br/> // unsigned decimal to binary (integer ), default Value: 32-bit </P> <p> // division algorithm <br/> Public static int [] unsigneddevision (INT dicinum) <br/>{< br/> int [] binary = new int [32]; // define a result set <br/> int consult; // define the integer quotient <br/> int remainder; // define the remainder <br/> int point = 31; </P> <p> for (INT I = 0; I <32; I ++) <br/>{< br/> binary [I] = 0; <br/>}< br/> consult = dicinum/2; <Br/> binary [point --] = dicinum % 2; </P> <p> while (Consult! = 0) <br/>{< br/> binary [point --] = consult % 2; <br/> consult/= 2; <br/>}</P> <p> return binary; <br/>}</P> <p> // Power Index algorithm </P> <p> Public static int [] unsignedpower (INT decinum) <br/>{< br/> int [] binary = new int [32]; // define the result set <br/> int Difference = decinum; // define the difference <br/> int flag = 0; // define the flag. If the power of 2 is less than the difference, the flag is 0, 1 </P> <p> for (INT I = 0; I <32; I ++) <br/>{< br/> binary [I] = 0; <br/>}</P> <p> for (INT I = 0; I <length; I ++) <br/>{< br/> If (difference> math. pow (2, LENGTH-1-i) <br/>{< br/> // system. out. println ("difference is bigger than math. pow (2, "+ (LENGTH-1-i) +"), and difference is: "+ difference); <br/> difference-= math. pow (2, LENGTH-1-i); <br/> binary [I] = 1; <br/>}< br/> else if (difference = math. pow (2, LENGTH-1-i) <br/>{< br/> // system. out. println ("difference is equals math. pow (2, "+ (LENGTH-1-i) +"), and difference is: "+ difference); <br/> binary [I] = 1; <br/> return binary; <br/>}< br/> else <br/> {<br/> // system. out. println ("difference is litter than math. pow (2, "+ (LENGTH-1-i) +"), and difference is: "+ difference); <br/> // difference-= math. pow (2, I); <br/>}</P> <p> return binary; <br/>}</P> <p> // convert signed integer to binary in decimal format, the default value is 32-bit. </P> <p> // The unsigned floating point is converted to binary in decimal format, the default value is 32-bit </P> <p> // converted to binary using a signed floating point in decimal format. The default value is 32-bit. <br/>}

 

There are some annotations at the end of the above dispute code, which are aimed at the algorithm for updating signed integers, unsigned floating-point numbers, and signed floating-point numbers. Here, I package it on MASM. Chapter1. If you need it, you can modify it as needed. The following is the test code:

Package MASM. test; import MASM. chapter1. *; public class decimaltobinarytest {public static void main (string [] ARGs) {// int [] result = decimaltobinary. unsigneddevision (10); int [] result = decimaltobinary. unsignedpower (256); For (INT I = 0; I <32; I ++) {if (I % 4 = 0) system. out. print (""); system. out. print (result [I]) ;}}

 

The test code is packaged on MASM. Test. You can modify the test code on your machine as needed.

Test results:

 

The above test value is applicable to the unsigned integer decimal to binary power index algorithm. It has been tested in the previous blog, and there are inevitably many shortcomings in the above, I hope you will not be enlightened, or optimize algorithms or more and better algorithms.

 

 

========================================================== ========================================================== =

2011-01-20

 

Sorry, I haven't updated it for a long time. I just put it in this way. This sort adds a negative number to the binary algorithm, which is represented by a complement code, the entire file structure here is as follows:

The process of compiling a Java file into a class file is not described in detail here. The focus is on the code algorithm. Here I only provide the source code of binarycompute. Java and decimaltobinary. Java:

Package MASM. chapter1; </P> <p>/** <br/> * binary computing applications <br/> * binarycompute. java <br/> * 2011-1-18 <br/> * Saturday <br/> */<br/> public class binarycompute <br/>{</P> <p> Public static int length = 32; </P> <p> // This is an integer array used to describe binary operations. <br/> // binary to decimal algorithm, for the efficiency of algorithm operation, <br/> Public static void getnot (INT [] binary) <br/>{< br/> for (INT I = 0; I <length; I ++) <br/> binary [I] = 1-binary [I]; <br/>}</P> <p> // binary addition 1 <br/> Public static void increase (INT [] binary) <br/> {<br/> int [] One = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}; <br/> Add (binary, one); <br/>}</P> <p> // Add two binary numbers, <br/> Public static void add (INT [] binary, int [] binary1) <br/> {<br/> int sum = 0; <br/> int carrynum = 0; // carry mark <br/> for (INT I = length-1; i> = 0; I --) <br/> {<br/> sum = binary [I] + binary1 [I] + carrynum; <br/> // If two corresponding binary phase addition numbers are 2, then we carry <br/> If (sum = 2) <br/>{< br/> carrynum = 1; <br/> binary [I] = 0; <br/>}< br/> // if not 2, there is no carry, that is, carrynum = 0. In this case, you need to reset the <br/> else <br/> {<br/> binary [I] = sum; <br/> carrynum = 0; <br/>}< br/>}

 

Package MASM. chapter1; </P> <p>/** <br/> * decimaltobinary. java <br/> * 2011-1-20 <br/> */</P> <p> Import MASM. chapter1.binarycompute; <br/> public class decimaltobinary <br/>{< br/> Public static int length = 32; <br/> // unsigned decimal to binary (integer). The default value is 32 bits, and the decimal range is 0 ~ 2 ^ 32-1 </P> <p> // division algorithm <br/> Public static int [] unsigneddevision (INT decinum) <br/>{< br/> int [] binary = new int [length]; // define a result set <br/> int consult; // define the integer quotient <br/> int remainder; // define the remainder <br/> int point = 31; </P> <p> for (INT I = 0; I <length; I ++) <br/>{< br/> binary [I] = 0; <br/>}< br/> consult = decinum/2; <br/> binary [point --] = decinum % 2; </P> <p> while (Consult! = 0) <br/>{< br/> binary [point --] = consult % 2; <br/> consult/= 2; <br/>}</P> <p> return binary; <br/>}</P> <p> // Power Index algorithm </P> <p> Public static int [] unsignedpower (INT decinum) <br/>{< br/> int [] binary = new int [length]; // define the result set <br/> int Difference = decinum; // define the difference <br/> int flag = 0; // define the flag. If the power of 2 is less than the difference, the flag is 0, 1 </P> <p> for (INT I = 0; I <length; I ++) <br/>{< br/> binary [I] = 0; <br/>}</P> <P> for (INT I = 0; I <length; I ++) <br/>{< br/> If (difference> math. pow (2, LENGTH-1-i) <br/>{< br/> // system. out. println ("difference is bigger than math. pow (2, "+ (LENGTH-1-i) +"), and difference is: "+ difference); <br/> difference-= math. pow (2, LENGTH-1-i); <br/> binary [I] = 1; <br/>}< br/> else if (difference = math. pow (2, LENGTH-1-i) <br/>{< br/> // system. out. println ("difference is equals math. pow (2, "+ (L ENGTH-1-i) + "), and difference is:" + difference); <br/> binary [I] = 1; <br/> return binary; <br/>}< br/> else <br/> {<br/> // system. out. println ("difference is litter than math. pow (2, "+ (LENGTH-1-i) +"), and difference is: "+ difference); <br/> // difference-= math. pow (2, I); <br/>}</P> <p> return binary; <br/>}</P> <p> // converted to binary using a signed integer in decimal format. The default value is 32 bits </P> <p> // division algorithm, the parameter is negative. <br/> Public static int [] signeddevis Ion (INT decinum) <br/>{< br/> int [] binary = new int [length]; <br/> If (decinum> = 0) <br/>{< br/> system. out. println ("the decimal number you entered is not a negative number! "); <Br/> return unsigneddevision (decinum); // if it is a positive number, directly use the unsigned positive number division algorithm <br/>}< br/> else <br/>{< br/> int strongdecinum =-decinum; // obtain the decinum positive value <br/> binary = unsigneddevision (strongdecinum); // obtain the binary value of stongdecinum <br/> binarycompute. getnot (Binary); // binary inversion <br/> binarycompute. increase (Binary); // then add 1. Overflow is not considered here <br/>}< br/> return binary; <br/>}</P> <p> // Power Index algorithm <br/> Public static int [] signedpower (INT decinum) <br/>{< br/> int [] binary = new int [length]; </P> <p> return binary; <br/>}</P> <p> // The unsigned floating point is converted to binary in decimal format, the default value is 32-bit </P> <p> // converted to binary using a signed floating point in decimal format. The default value is 32-bit <br/>}< br/>

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.