09 June, 2013

Bitwise 的各種運算

Standard

Code:

  1. // ap8,Bitwise 的各種運算
  2.  
  3. public class ap8
  4. {
  5. public static void main(String args[])
  6. {
  7. int i=13;
  8. int j=14;
  9. int k=i+j;
  10. short l=27;
  11. String x = "0000000000000000000000000000000000";
  12. String y = "1111111111111111111111111111111111";
  13. String z = "0101010101010100101010101010101010";
  14. String w = "1010101010101011010101010101010101";
  15. String pad = "--------------------------------";
  16. x = Integer.toBinaryString(i);
  17. y = Integer.toBinaryString(j);
  18. z = Integer.toBinaryString(k);
  19. w = Integer.toBinaryString(l);
  20. System.out.println("變數值...");
  21. System.out.println("i = " + i + " ~i = " + ~i);
  22. System.out.println("j = " + j + " ~j = " + ~j);
  23. System.out.println("k = " + k + " ~k = " + ~k);
  24. System.out.println("l = " + l + " ~l = " + ~l);
  25. System.out.println("二進位數值輸出: 變數值...");
  26. System.out.println("i = " + x);
  27. System.out.println("j = " + y);
  28. System.out.println("k = " + z);
  29. System.out.println("l = " + w);
  30. x = Integer.toBinaryString(~i);
  31. y = Integer.toBinaryString(~j);
  32. z = Integer.toBinaryString(~k);
  33. w = Integer.toBinaryString(~l);
  34. System.out.println("~i = " + x);
  35. System.out.println("~j = " + y);
  36. System.out.println("~k = " + z);
  37. System.out.println("~l = " + w);
  38. System.out.println();
  39. System.out.println("位元運算...");
  40. System.out.println("i & j = " + (i & j)); // AND
  41. x = Integer.toBinaryString(i);
  42. y = Integer.toBinaryString(j);
  43. z = Integer.toBinaryString(i & j);
  44. System.out.println("二進位數值輸出: AND...");
  45. System.out.println(x+'\n'+y+'\n'+pad+'\n'+z);
  46. System.out.println();
  47. System.out.println("i | j = " + (i | j)); // OR
  48. x = Integer.toBinaryString(i);
  49. y = Integer.toBinaryString(j);
  50. z = Integer.toBinaryString(i | j);
  51. System.out.println("二進位數值輸出: OR...");
  52. System.out.println(x+'\n'+y+'\n'+pad+'\n'+z);
  53. System.out.println();
  54. System.out.println("i ^ j = " + (i ^ j)); // XOR
  55. x = Integer.toBinaryString(i);
  56. y = Integer.toBinaryString(j);
  57. z = Integer.toBinaryString(i ^ j);
  58. System.out.println("二進位數值輸出: XOR...");
  59. System.out.println(x+'\n'+y+'\n'+pad+'\n'+z);
  60. System.out.println();
  61. System.out.println("~i + ~j = " + (~i+~j)); // 補數
  62. System.out.println("二進位數值輸出: NOT...");
  63. x = Integer.toBinaryString(i);
  64. y = Integer.toBinaryString(j);
  65. System.out.println("~"+x+" + "+"~"+y+" = ");
  66. x = Integer.toBinaryString(~i);
  67. y = Integer.toBinaryString(~j);
  68. z = Integer.toBinaryString(~i + ~j);
  69. System.out.println(x+'\n'+y+'\n'+pad+'\n'+z);
  70. System.out.println();
  71. //System.out.println("l = i+j <<< 1 = " + (l<<<1)); // 無號數不能左移
  72. System.out.println("l = i+j >>> 1 = " + (l>>>1)); // 無號數/2
  73. System.out.println("二進位數值輸出: 無號數 >>> 1...");
  74. x = Integer.toBinaryString(i);
  75. y = Integer.toBinaryString(j);
  76. System.out.println(x+" + "+y+" = ");
  77. x = Integer.toBinaryString(l);
  78. y = Integer.toBinaryString(l>>>1);
  79. System.out.println(x+" >>> 1"+'\n'+pad+'\n'+y);
  80. System.out.println();
  81. System.out.println("-(i+j) << 1 = " + (-(k)<<1)); // 有號數*2
  82. System.out.println("二進位數值輸出: 有號數 << 1...");
  83. x = Integer.toBinaryString(i);
  84. y = Integer.toBinaryString(j);
  85. z = Integer.toBinaryString(i+j);
  86. System.out.println("-"+'('+x+" + "+y+')'+" = " +'-'+z);
  87. x = Integer.toBinaryString(-k);
  88. y = Integer.toBinaryString(-k<<1);
  89. System.out.println(x+" << 1"+'\n'+pad+'\n'+y);
  90. System.out.println();
  91. System.out.println("-(i+j) >> 1 = " + ((-k)>>1)); // 有號數/2
  92. System.out.println("二進位數值輸出: 有號數 >> 1...");
  93. x = Integer.toBinaryString(i);
  94. y = Integer.toBinaryString(j);
  95. z = Integer.toBinaryString(i+j);
  96. System.out.println("-"+'('+x+" + "+y+')'+" = "+'-'+z);
  97. x = Integer.toBinaryString(-k);
  98. y = Integer.toBinaryString(-k>>1);
  99. System.out.println(x+" >> 1"+'\n'+pad+'\n'+y);
  100.  
  101. /* 001101 取 NOT => 110010(2補)
  102. * 110010-1=110001(1補)
  103. * 110001 再取 NOT => 001110(14),最後加上負號
  104. */
  105. }
  106. }

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:

Related Posts:

  • Bitwise 的各種運算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… Read More
  • 幽默的代碼註解程序源代碼中的註釋經常是一個臥虎藏龍的地方,來看看這一輯國外某公司產品中的註釋。注意:看的時候嚴禁喝水或進食。 親愛的代碼維護人員: 當您嘗試優化這段代碼但發現這是一個極端錯誤的決定的時候,請修改下面的計時器,以便警示後人。 總計浪費在這段代碼的時間 = 16小時(喂~後面那個傻逼!) 真… Read More
  • 程序人生演講內容摘要 ---侯捷如果你不曾聽過侯捷的名字,不曾知道侯捷做的事情,你不可能有興趣走入會場。因此,各位遠道而來,我竊以為,無非想看看侯捷本人,聽聽他說話。如果你期盼在這種場合聽到某某技術的剖析,某某趨勢的發展,肯定你會失望。我不是趨勢專家,對此也毫無興趣。台上說話和台下聊天不同,我不能也不敢講我沒有心得沒有研究的話題。… Read More
  • 運算式的型態轉換Code: // ap6,運算式的型態轉換 public class ap6 { public static void main(String args[]) { short s=-5; int i=6; float f=9.67f; … Read More
  • 運算式真假值Code: // ap7,運算式真假值 public class ap7 { public static void main(String args[]) { int a=126; int b=-2*a; boolean c=true; boolean d… Read More

0 comments: