convert decimal number to binary in Java
The first one: except for the basis of the reverse method
This is the most consistent with our usual mathematical logical thinking, that is, enter a decimal number n, each time with n divided by 2, the remainder of the note down, and then use the quotient to remove the 2 ... Loops sequentially, until the quotient is 0, and the remainder is arranged in order, constitutes the converted binary number.
Then, in the actual implementation, you can use a number of int to store the last binary, each time after the remainder store in the number of INT-type low, sequentially increment.
1 public void Binarytodecimal (int n) {
2 int t = 0; Used to record
the number of digits 3 int bin = 0;//to record the last binary number
4 int r = 0; Used to store the remainder
5 while (n!= 0) {
6 r = n% 2;
7 n = n/2;
8 bin = R * Math (). POW (10,t);
9 t++;
One System.out.println (bin);
The }
However, the largest int can only represent 2^31-1 positive numbers, so the stored binary digits are limited; as we all know, int in Java storage range is 32 bits, you can use string concatenation (+) to implement, the code is as follows: Press CTRL + C to copy the Code public void Binarytodecimal (int n) {String str = ""; while (n!=0) {str = N%2+STR; n = n/2;} System.out.println (str); Press CTRL + C to copy the code
The second: Using the "shift" operation to implement
We can shift the decimal number directly using the shift operation, that is: Move the highest number of digits to the lowest (31-bit), in addition to the lowest bit of the rest of the zero, use & operation, you can use and 1 phase (&), because 1 in memory in addition to the lowest bit is 1, the remaining 31 bits are zero, Then put this number in decimal output, and then move the high, do the same operation, until the last one, the code is as follows. It can be said that this is the simplest way I have seen so far.
1 public void Binarytodecimal (int n) {
2 for (int i = 31;i >= 0; i--)
3 System.out.print (n >>> I & 1);
4}
Note: Because the computer is stored in the number of the complement, the original code, the inverse code, the complement is the same, and negative number of the original code, the inverse code, the complement is not the same, complement = original code to take +1 (symbol bit unchanged). So, the negative number is output according to its complement.
>>> for logical shift character, move n position to right, high 0
>> arithmetic shift, is also shifted to the right n bit, the difference is: High positive 0, negative high 1
<< shift character, move n position to left, low fill 0
Third: Calling API functions
This is a more object-oriented approach to dealing with problems:
1 public void function1 (int n) {
2 String result = integer.tobinarystring (n);
3 //int r = integer.parseint (result);
4 //system.out.println (r);
5 System.out.println (result);
6 }
Small suggestion: This code can be directly output by string, you can also use Interger.parseint () to convert to int, but this method is not recommended, when a negative number, int can not represent 32 of an integer