Integer's toBinaryString method in java_Intefrankly

Integer's toBinaryString method in java

During an interview, I came across this question, the general meaning of the question is: let write the toBinaryString() method in the Integer class

That is, write out the process of converting Integer to Binary

But I was blinded, and after checking out the JDK source code, I found this great way to use it, and I'm showing it to the guys here

Here is a test I did.

``` 1 /**
2  *
3  */
4 package com.b510.test;
5
6 /**
7  * @author Hongten
8  * @date 2013-12-15
9  */
10 public class TestF {
11
12     public static void main(String[] args) {
13         //output:1000
14         System.out.println(toBinaryString(8));
15         //printInfo();
16     }
17
18     /**
19      * Here's the do& Test of operation， that is say， (located) at1&*（ among others* Represents other figures， as if：0,1,2,3,4...） When operating
20      *  They are performed between the binary&( together with) arithmetic operation。 Only if* odd-numbered（1,3,5,7...） at the time of，1*& operation before it can return：1
21 * Other cases returned: 0
22      */
23     private static void printInfo(){
24         for(int i =0; i< 10; i++){
25             System.out.println("i= " + i + "         "+(i & 1));
26         }
27         /*
28         output:
29         i= 0         0
30         i= 1         1
31         i= 2         0
32         i= 3         1
33         i= 4         0
34         i= 5         1
35         i= 6         0
36         i= 7         1
37         i= 8         0
38         i= 9         1
39         */
40     }
41
42     public static String toBinaryString(int i) {
44     }
45
46     /**
47      * Convert the integer to an unsigned number.
48      */
49     private static String toUnsignedString(int i, int shift) {
50         char[] buf = new char[32];
51         int charPos = 32;
52         int radix = 1 << shift;
54         do {
55             // Here.mask have been working for：1， So wheni When it is an odd number， here"i & mask" The operation is only：1
56 // otherwise return: 0
58             buf[--charPos] = digits[i & mask];
59             i >>>= shift;// Right shift assignment， The empty space on the left is marked by0 padding
60         //System.out.println(buf);
61         //System.out.println(charPos);
62         //System.out.println(i);
63         } while (i != 0);
64         return new String(buf, charPos, (32 - charPos));
65     }
66
67     final static char[] digits = {
68         '0' , '1' , '2' , '3' , '4' , '5' ,
69         '6' , '7' , '8' , '9' , 'a' , 'b' ,
70         'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
71         'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
72         'o' , 'p' , 'q' , 'r' , 's' , 't' ,
73         'u' , 'v' , 'w' , 'x' , 'y' , 'z'
74         };
75 }```

In the code, we can actually simplify the digits array since we will only use the arrays: digits[0],digits[1]

So.

```1 final static char[] digits = {
2         '0' , '1'
3         };```

method uses the shift operation and the& operations， These two operations are key。

E | hongtenzone@foxmail.com B |http://www.cnblogs.com/hongten

Recommended>>
1、Go and HTTPS 1
2、Programming LanguagesCore Concepts04 Data Types
3、Python gets the binary value of a number
4、Simple use of yum
5、With the post00s on the rise what exactly are undergraduates in AI majors starting this fall studying

已推荐到看一看 和朋友分享想法
最多200字，当前共 发送

已发送

确定
分享你的想法...
取消

确定
最多200字，当前共

发送中