1. 程式人生 > >!!無判斷求兩數中較大數(-1的位運算)

!!無判斷求兩數中較大數(-1的位運算)

轉自:https://bbs.csdn.net/topics/390957891

public static void main(String[] args) {
    //-1>>3 = -1,-1>>>3 = 536870911
 
    //1、取-1的絕對值1的二進位制00000000000000000000000000000001(以4個位元組表示)
    //2、取1的反碼11111111111111111111111111111110
    //3、取-1的二進位制11111111111111111111111111111111(反碼+1)
    //4、>>有符號右移3為,最後得到的結果11111111111111111111111111111111(換位10進位制還是為-1)
    //5、>>>無符號右移,最後得到的結果00011111111111111111111111111111(等比數列求和2^29,結果為536870911)
     
    System.out.println(-1>>3);
    System.out.println(-1>>>3);
    System.out.println("-1的二進位制程式碼:"+Integer.toBinaryString(-1));
    System.out.println("-1有符號右移3位的二進位制碼:"+Integer.toBinaryString(-1>>3));
    System.out.println("-1無符號右移3位的二進位制碼:"+Integer.toBinaryString(-1>>>3));
    String strUnsigned="000"+Integer.toBinaryString(-1>>>3);
    BigInteger resultHex= new BigInteger(strUnsigned,2);
    System.out.println("-1無符號右移3位的十進位制:"+resultHex);
}