09 June, 2013

Bitwise 的各種運算

Standard

Code:

// ap8,Bitwise 的各種運算

public class ap8
{
  public static void main(String args[])
  {
    int i=13;
    int j=14;
    int k=i+j;
    short l=27;
    String x = "0000000000000000000000000000000000";
    String y = "1111111111111111111111111111111111";
    String z = "0101010101010100101010101010101010";
    String w = "1010101010101011010101010101010101";
    String pad = "--------------------------------";
    x = Integer.toBinaryString(i);
    y = Integer.toBinaryString(j);
    z = Integer.toBinaryString(k);
    w = Integer.toBinaryString(l);
    
    System.out.println("變數值...");
    System.out.println("i = " + i + " ~i = " + ~i);
    System.out.println("j = " + j + " ~j = " + ~j);
    System.out.println("k = " + k + " ~k = " + ~k);
    System.out.println("l = " + l + " ~l = " + ~l);
    System.out.println("二進位數值輸出: 變數值...");
    System.out.println("i = " + x);
    System.out.println("j = " + y);
    System.out.println("k = " + z);
    System.out.println("l = " + w);
    x = Integer.toBinaryString(~i);
    y = Integer.toBinaryString(~j);
    z = Integer.toBinaryString(~k);
    w = Integer.toBinaryString(~l);
    System.out.println("~i = " + x);
    System.out.println("~j = " + y);
    System.out.println("~k = " + z);
    System.out.println("~l = " + w);
 System.out.println();
 
    System.out.println("位元運算...");
    System.out.println("i & j = " + (i & j));  // AND
    x = Integer.toBinaryString(i);
    y = Integer.toBinaryString(j);
    z = Integer.toBinaryString(i & j);
    System.out.println("二進位數值輸出: AND...");
    System.out.println(x+'\n'+y+'\n'+pad+'\n'+z);
    System.out.println();
    
    System.out.println("i | j = " + (i | j));  // OR
    x = Integer.toBinaryString(i);
    y = Integer.toBinaryString(j);
    z = Integer.toBinaryString(i | j);
    System.out.println("二進位數值輸出: OR...");
    System.out.println(x+'\n'+y+'\n'+pad+'\n'+z);
    System.out.println();
    
    System.out.println("i ^ j = " + (i ^ j));  // XOR
    x = Integer.toBinaryString(i);
    y = Integer.toBinaryString(j);
    z = Integer.toBinaryString(i ^ j);
    System.out.println("二進位數值輸出: XOR...");
    System.out.println(x+'\n'+y+'\n'+pad+'\n'+z);
    System.out.println();
    
    System.out.println("~i + ~j = " + (~i+~j));  // 補數
    System.out.println("二進位數值輸出: NOT...");
    x = Integer.toBinaryString(i);
    y = Integer.toBinaryString(j);
    System.out.println("~"+x+" + "+"~"+y+" = ");
    x = Integer.toBinaryString(~i);
    y = Integer.toBinaryString(~j);
    z = Integer.toBinaryString(~i + ~j);
    System.out.println(x+'\n'+y+'\n'+pad+'\n'+z);
    System.out.println();
    
    //System.out.println("l = i+j <<< 1 = " + (l<<<1));   // 無號數不能左移
    System.out.println("l = i+j >>> 1 = " + (l>>>1));     // 無號數/2
    System.out.println("二進位數值輸出: 無號數 >>> 1...");
    x = Integer.toBinaryString(i);
    y = Integer.toBinaryString(j);
    System.out.println(x+" + "+y+" = ");
    x = Integer.toBinaryString(l);
    y = Integer.toBinaryString(l>>>1);
    System.out.println(x+" >>> 1"+'\n'+pad+'\n'+y);
    System.out.println();
    
    System.out.println("-(i+j) << 1 = " + (-(k)<<1));  // 有號數*2
    System.out.println("二進位數值輸出: 有號數 << 1...");
    x = Integer.toBinaryString(i);
    y = Integer.toBinaryString(j);
    z = Integer.toBinaryString(i+j);
    System.out.println("-"+'('+x+" + "+y+')'+" = " +'-'+z);
    x = Integer.toBinaryString(-k);
    y = Integer.toBinaryString(-k<<1);
    System.out.println(x+" << 1"+'\n'+pad+'\n'+y);
    System.out.println();
    
    System.out.println("-(i+j) >> 1 = " + ((-k)>>1));  // 有號數/2
    System.out.println("二進位數值輸出: 有號數 >> 1...");
    x = Integer.toBinaryString(i);
    y = Integer.toBinaryString(j);
    z = Integer.toBinaryString(i+j);
    System.out.println("-"+'('+x+" + "+y+')'+" = "+'-'+z);
    x = Integer.toBinaryString(-k);
    y = Integer.toBinaryString(-k>>1);
    System.out.println(x+" >> 1"+'\n'+pad+'\n'+y);

    /* 001101 取 NOT => 110010(2補)
     * 110010-1=110001(1補)
     * 110001 再取 NOT => 001110(14),最後加上負號
     */
  }
}

Output:

變數值...
i = 13 ~i = -14
j = 14 ~j = -15
k = 27 ~k = -28
l = 27 ~l = -28
二進位數值輸出: 變數值...
i = 1101
j = 1110
k = 11011
l = 11011
~i = 11111111111111111111111111110010
~j = 11111111111111111111111111110001
~k = 11111111111111111111111111100100
~l = 11111111111111111111111111100100

位元運算...
i & j = 12
二進位數值輸出: AND...
1101
1110
--------------------------------
1100

i | j = 15
二進位數值輸出: OR...
1101
1110
--------------------------------
1111

i ^ j = 3
二進位數值輸出: XOR...
1101
1110
--------------------------------
11

~i + ~j = -29
二進位數值輸出: NOT...
~1101 + ~1110 =
11111111111111111111111111110010
11111111111111111111111111110001
--------------------------------
11111111111111111111111111100011

l = i+j >>> 1 = 13
二進位數值輸出: 無號數 >>> 1...
1101 + 1110 =
11011 >>> 1
--------------------------------
1101

-(i+j) << 1 = -54
二進位數值輸出: 有號數 << 1...
-(1101 + 1110) = -11011
11111111111111111111111111100101 << 1
--------------------------------
11111111111111111111111111001010

-(i+j) >> 1 = -14
二進位數值輸出: 有號數 >> 1...
-(1101 + 1110) = -11011
11111111111111111111111111100101 >> 1
--------------------------------
11111111111111111111111111110010
Press any key to continue...

Discuss:

0 comments: