Integer.java#toUnsignedString please explain

KasynoHamn

New Member
I am trying to wrap my head on bitwise operations in java and its practical application and uses and i found this when i was browsing java.lang.Integer.java.Please bear with me if the questions sound very basic.Could anyone explain step by step what happens in toUnsignedString method.Questions are inline in the below code as inline comments.Code\[code\]public class TestMain { public static void main(String[] args) { System.out.println(Integer.toBinaryString(Integer.MAX_VALUE)); System.out.println(Integer.toBinaryString(Integer.MAX_VALUE+1)); System.out.println(toUnsignedString(Integer.MAX_VALUE+1, 4)); } /** * All possible chars for representing a number as a String */ final static char[] digits = { '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' }; /** * Convert the integer to an unsigned number. */ private static String toUnsignedString(int i, int shift) { char[] buf = new char[32]; int charPos = 32; int radix = 1 << shift; int mask = radix - 1; do { buf[--charPos] = digits[i & mask];//what purpose does it serve masking i ? wont this only return 15 or 0 for all possible values of i i >>>= shift; //Why right shift here , and why right shift by 'shift' value ? } while (i != 0); return new String(buf, charPos, (32 - charPos)); }}\[/code\]
 
Back
Top