During an interview, I encountered such a question. The question probably meant to write the toBinaryString () method in the Integer class.
That is to say, the process of converting Integer to Binary is written.
But I did not, I checked the JDK source code and found this very useful method. Let's take a look at it here.
The following is a test I conducted:
System..println(toBinaryString( ( i =; i< ; i++ System..println( + i + +(i & String toBinaryString( toUnsignedString(i, String toUnsignedString( i, [] buf = [ charPos = radix = << mask = radix - buf[--charPos] = digits[i & i >>>= shift; } (i != String(buf, charPos, ( - final [] digits = , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , }
In the code, we can actually simplify the digits array, because we only use the array: digits [0], digits [1]
Therefore:
[] digits = '0' , '1' };
The shift operation and & operation are used in the method. These two operations are the key.