java移位操作示例
public class Shift4JTest {
public static void main(String[] args) {
int a = 10;
int b = a << 1;
int c = a << 2;
int d = a << 3;
BitUtil.getFull32BinaryString(a);
System.out.println(b);
BitUtil.getFull32BinaryString(b);
System.out.println(c);
BitUtil.getFull32BinaryString(c);
System.out.println(d);
BitUtil.getFull32BinaryString(d);
}
public class BitUtil {
public static String getFull32BinaryString(Integer a){
String s = Integer.toBinaryString(a);
int count = 32 - s.length();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < count; i++) {
sb.append("0");
if (i != 0 && i % 8 == 0) sb.append(" ");
}
sb.append(s);
System.out.println(sb.toString());
return sb.toString();
}
}
}
int a = 80;
int b = a >> 1;
int c = a >> 2;
int d = a >> 3;
System.out.println(a);
BitUtil.getFull32BinaryString(a);
System.out.println(b);
BitUtil.getFull32BinaryString(b);
System.out.println(c);
BitUtil.getFull32BinaryString(c);
System.out.println(d);
BitUtil.getFull32BinaryString(d);
int a = -80;
int b = a >> 1;
int c = a >> 2;
int d = a >> 3;
System.out.println(a);
BitUtil.getFull32BinaryString(a);
System.out.println(b);
BitUtil.getFull32BinaryString(b);
System.out.println(c);
BitUtil.getFull32BinaryString(c);
System.out.println(d);
BitUtil.getFull32BinaryString(d);
int a = 80;
int b = a >>> 1;
int c = a >>> 2;
int d = a >>> 3;
System.out.println(a);
BitUtil.getFull32BinaryString(a);
System.out.println(b);
BitUtil.getFull32BinaryString(b);
System.out.println(c);
BitUtil.getFull32BinaryString(c);
System.out.println(d);
BitUtil.getFull32BinaryString(d);
int a = -80;
int b = a >>> 1;
int c = a >>> 2;
int d = a >>> 3;
System.out.println(a);
BitUtil.getFull32BinaryString(a);
System.out.println(b);
BitUtil.getFull32BinaryString(b);
System.out.println(c);
BitUtil.getFull32BinaryString(c);
System.out.println(d);
BitUtil.getFull32BinaryString(d);
|