1.取模 (a mod b)
System.out.println("10 mod 3 = " + Math.floorMod(10, 3));
System.out.println("10 mod (-3) = " + Math.floorMod(10, -3));
System.out.println("(-10) mod 3 = " + Math.floorMod(-10, 3));
System.out.println("(-10) mod (-3) = " + Math.floorMod(-10, -3));
结果
结论
取模运算结果的符号与b的符号一致。
2.取余(a % b)
System.out.println("10 % 3 = " + (10 % 3));
System.out.println("10 % (-3) = " + (10 % (-3)));
System.out.println("(-10) % 3 = " + ((-10) % 3));
System.out.println("(-10) % (-3) = " + ((-10) % (-3)));
结果
结论
取余运算结果的符号与a的符号一致。
3.左移(a << b)
// -----------integer--------------
System.out.println("10 << 3 = " + (10 << 3));
System.out.println("10 << (-3) = " + (10 << (-3)));
System.out.println("10 << 29 = " + (10 << 29));
System.out.println("(-10) << 3 = " + ((-10) << 3));
System.out.println("(-10) << (-3) = " + ((-10) << (-3)));
System.out.println("(-10) << 29 = " + ((-10) << 29));
// ----------- long --------------
System.out.println("10L << 3 = " + (10L << 3));
System.out.println("10L << (-3) = " + (10L << (-3)));
System.out.println("10L << 61 = " + (10L << (-3)));
System.out.println("(-10L) << 3 = " + ((-10L) << 3));
System.out.println("(-10L) << (-3) = " + ((-10L) << (-3)));
System.out.println("(-10L) << 61 = " + ((-10L) << (-3)));
结果
结论
a扩大相应倍数,且结果符号保持不变右侧补0,当b<0时,取(b mod 32)或(b mod 64)。
4.右移(a >> b)
// -----------integer--------------
System.out.println("10 >> 3 = " + (10 >> 3));
System.out.println("10 >> (-3) = " + (10 >> (-3)));
System.out.println("10 >> 29 = " + (10 >> 29));
System.out.println("(-10) >> 3 = " + ((-10) >> 3));
System.out.println("(-10) >> (-3) = " + ((-10) >> (-3)));
System.out.println("(-10) >> 29 = " + ((-10) >> 29));
// ----------- long --------------
System.out.println("10L >> 3 = " + (10L >> 3));
System.out.println("10L >> (-3) = " + (10L >> (-3)));
System.out.println("10L >> 61 = " + (10L >> (-3)));
System.out.println("(-10L) >> 3 = " + ((-10L) >> 3));
System.out.println("(-10L) >> (-3) = " + ((-10L) >> (-3)));
System.out.println("(-10L) >> 61 = " + ((-10L) >> (-3)));
结果
结论
a缩小相应倍数,且结果符号保持不变左侧补0,当b<0时,取(b mod 32)或(b mod 64)。
5.无符号右移(a >>> b)
// -----------integer--------------
System.out.println("10 >>> 3 = " + (10 >>> 3));
System.out.println("10 >>> (-3) = " + (10 >>> (-3)));
System.out.println("10 >>> 29 = " + (10 >>> 29));
System.out.println("(-10) >>> 3 = " + ((-10) >>> 3));
System.out.println("(-10) >>> (-3) = " + ((-10) >>> (-3)));
System.out.println("(-10) >>> 29 = " + ((-10) >>> 29));
// ----------- long --------------
System.out.println("10L >>> 3 = " + (10L >>> 3));
System.out.println("10L >>> (-3) = " + (10L >>> (-3)));
System.out.println("10L >>> 61 = " + (10L >>> (-3)));
System.out.println("(-10L) >>> 3 = " + ((-10L) >>> 3));
System.out.println("(-10L) >>> (-3) = " + ((-10L) >>> (-3)));
System.out.println("(-10L) >>> 61 = " + ((-10L) >>> (-3)));
结果
结论
a缩小相应倍数,左侧所有位补0,当b<0时,取(b mod 32)或(b mod 64)。
6.其他整数的位移(char、byte、short)
System.out.println("(char)10 << (-3) = " + ((char)10 << (-3)));
System.out.println("(byte)10 << (-3) = " + ((byte)10 << (-3)));
System.out.println("(short)10 << (-3) = " + ((short)10 << (-3)));
System.out.println("10 << (-3) = " + (10 << (-3)));
System.out.println("10 << 29 = " + (10 << 29));
结果
结论