二进制转十进制的思路

【二进制转十进制的思路】public class ToBin {
public static String toBinString1(int i) {
return Integer.toBinaryString(i);
}
public static String toBinString2(int i) {
//int 型是4个字节,32位,也就是说左移32次,每次移1位
StringBuilder builder = new StringBuilder();
int count = 32;
while(count-- > 0) {
builder.append((i & 1) == 1 ? "1":"0");
i = i>>1;
}
return builder.reverse().toString();
}
public static void main(String[] args) {
System.out.println(toBinString1(100));
System.out.println(toBinString2(100));
}
}

    推荐阅读