乐趣区

关于二进制:二进制转十进制的思路

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));
}
}

退出移动版