java 位操作 bitwise(按位) operation bit

來源:互聯網
上載者:User

標籤:

java 位操作 bitwise(按位) operation bit

// 8   0000 0000 0000 1000     原碼
      1111 1111 1111 0111     反碼

+              1

   1111 1111 1111 1000     (8的補碼)來表示 -8

 

// -8 1111 1111 1111 1000 65528     補碼(正值 的反碼+1)

// 65535 1111 1111 1111 111165535
// 65535-65528=7+1=8

 操作longValue = longValue | (1 << n); 可以在longValue的2進位表示中,把從右邊數到左邊數第n + 1位的值設定為1,並且不影響其他位上面的值   即用0和2進位值變數x做或|or操作,不會影響到2進位變數x的值 

操作longValue = longValue & ~(1 << n); 可以在longValue的2進位表示中,把從右邊數到左邊數第n + 1位設定為0,並且不影響其他位上面的值   即用1和2進位值變數x做與&and操作,不會影響到2進位變數x的值 

操作System.out.println((longValue >> n & 1) == 1); 可以判斷值longValue的2進位表示中,從右邊數到左邊第n + 1位的值是0false 還是1true 

Java代碼  
  1. public class bitOperation {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      */  
  6.     public static void main(String[] args) {  
  7.           
  8.         long longValue = 0;  
  9.           
  10.         longValue = longValue | (1 << 0);  
  11.         // 1  
  12.         System.out.println(Long.toBinaryString(longValue));  
  13.         longValue = longValue | (1 << 1);  
  14.         // 11  
  15.         System.out.println(Long.toBinaryString(longValue));  
  16.         longValue = longValue | (1 << 4);  
  17.         // 10011  
  18.         System.out.println(Long.toBinaryString(longValue));  
  19.         longValue = longValue | (1 << 5);  
  20.         // 110011  
  21.         System.out.println(Long.toBinaryString(longValue));  
  22.         longValue = longValue | (1 << 6);  
  23.         // 1110011  
  24.         System.out.println(Long.toBinaryString(longValue));  
  25.   
  26.         String hex = Long.toBinaryString(longValue);  
  27.         // 1110011  
  28.         System.out.println(hex);  
  29.         // 115  
  30.         System.out.println(Integer.valueOf("1110011", 2));  
  31.         // 1110011  
  32.         System.out.println(Long.toBinaryString(longValue >> 0));  
  33.         // 1  
  34.         System.out.println(Long.toBinaryString(longValue >> 0 & 1));  
  35.         // 111001  
  36.         System.out.println(Long.toBinaryString(longValue >> 1));  
  37.         // 1  
  38.         System.out.println(Long.toBinaryString(longValue >> 1 & 1));  
  39.         // true  
  40.         System.out.println((longValue >> 0 & 1) == 1);  
  41.         // true  
  42.         System.out.println((longValue >> 1 & 1) == 1);  
  43.         // false  
  44.         System.out.println((longValue >> 2 & 1) == 1);  
  45.         // false  
  46.         System.out.println((longValue >> 3 & 1) == 1);  
  47.         // true  
  48.         System.out.println((longValue >> 4 & 1) == 1);  
  49.         // true  
  50.         System.out.println((longValue >> 5 & 1) == 1);  
  51.         // true  
  52.         System.out.println((longValue >> 6 & 1) == 1);  
  53.         // false  
  54.         System.out.println((longValue >> 7 & 1) == 1);  
  55.   
  56.         // Demonstrate the bitwise logical operators.  
  57.         bitLogic();  
  58.         // Left shifting a byte value.  
  59.         byteShift();  
  60.     }  
  61.   
  62.     /** 
  63.      * Left shifting a byte value. 
  64.      */  
  65.     private static void byteShift() {  
  66.         byte a = 64, b;  
  67.         int i;  
  68.   
  69.         i = a << 2;  
  70.         b = (byte) (a << 2);  
  71.   
  72.         // Original value of a: 64  
  73.         System.out.println("Original value of a: " + a);  
  74.         // i and b: 256 0  
  75.         System.out.println("i and b: " + i + " " + b);  
  76.         System.out.println("\r\n");  
  77.     }  
  78.   
  79.     /** 
  80.      * Demonstrate the bitwise logical operators. 
  81.      */  
  82.     private static void bitLogic() {  
  83.         String binary[] = { "0000", "0001", "0010", "0011", "0100", "0101",  
  84.                 "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101",  
  85.                 "1110", "1111"  
  86.   
  87.         };  
  88.         int a = 3; // 0 + 2 + 1 or 0011 in binary  
  89.         int b = 6; // 4 + 2 + 0 or 0110 in binary  
  90.         int c = a | b;  
  91.         int d = a & b;  
  92.         int e = a ^ b;  
  93.         int f = (~a & b) | (a & ~b);  
  94.         int g = ~a & 0x0f;  
  95.   
  96.         // a = 0011 = 3  
  97.         System.out.println(" a = " + binary[a] + " = " + a);  
  98.         // b = 0110 = 6  
  99.         System.out.println(" b = " + binary[b] + " = " + b);  
  100.         // a|b = 0111 = 7  
  101.         System.out.println(" a|b = " + binary[c] + " = " + c);  
  102.         // a&b = 0010 = 2  
  103.         System.out.println(" a&b = " + binary[d] + " = " + d);  
  104.         // a^b = 0101 = 5  
  105.         System.out.println(" a^b = " + binary[e] + " = " + e);  
  106.         // ~a&b|a&~b = 0101 = 5  
  107.         System.out.println("~a&b|a&~b = " + binary[f] + " = " + f);  
  108.         // ~a = 1100 = 12  
  109.         System.out.println(" ~a = " + binary[g] + " = " + g);  
  110.         System.out.println("\r\n");  
  111.     }  
  112. }  

java 位操作 bitwise(按位) operation bit

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.