/**
* Created by tanmingxin on 2017/1/9.
*/
public class Test1 {
public static void main(String[] args) {
int number = -100000;
int bits = 2;
// test1(number, bits); // >>
/*
10
1010
2
10
>> 正数 整体移动 左补0
负数 整体移动 左补1
*/
// test2(number, bits); // <<
/*
10
1010
40
101000
<< 整体移动 右补0
*/
test3(number, bits);
/*
>>> 正数移动同 >> 整体移动 左补0
负数移动变成正数 整体移动 左边补0
*/
}
public static void printBinaryStr(int number) {
System.out.println(Integer.toBinaryString(number));
}
public static void printDecimalStr(int number) {
System.out.println(Integer.toString(number));
}
// >>
public static void test1(int number, int bits) {
printDecimalStr(number);
printBinaryStr(number);
number = number >> bits;
printDecimalStr(number);
printBinaryStr(number);
}
// <<
public static void test2(int number, int bits) {
printDecimalStr(number);
printBinaryStr(number);
number = number << bits;
printDecimalStr(number);
printBinaryStr(number);
}
// >>>
public static void test3(int number, int bits) {
printDecimalStr(number);
printBinaryStr(number);
number = number >>> bits;
printDecimalStr(number);
printBinaryStr(number);
}
}
最后修改:2019 年 02 月 23 日
© 允许规范转载