码迷,mamicode.com
首页 > 编程语言 > 详细

java 位操作 bitwise(按位) operation bit

时间:2015-06-12 16:44:47      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

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 1111 65535
// 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

标签:

原文地址:http://www.cnblogs.com/rojas/p/4571879.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!