java 位操作 bitwise(按位) operation bit
//一篇对于 原码 反码 补码 的介绍
http://www.cnblogs.com/zhangziqiu/archive/2011/03/30/ComputerCode.html
java中有三种移位运算符
<< : 左移运算符,num << 1,相当于num乘以2
>> : 右移运算符,num >> 1,相当于num除以2
>>> : 无符号右移,忽略符号位,空位都以0补齐
// 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
操作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
- public class bitOperation {
- /**
- * @param args
- */
- public static void main(String[] args) {
- long longValue = 0;
- longValue = longValue | (1 << 0);
- // 1
- System.out.println(Long.toBinaryString(longValue));
- longValue = longValue | (1 << 1);
- // 11
- System.out.println(Long.toBinaryString(longValue));
- longValue = longValue | (1 << 4);
- // 10011
- System.out.println(Long.toBinaryString(longValue));
- longValue = longValue | (1 << 5);
- // 110011
- System.out.println(Long.toBinaryString(longValue));
- longValue = longValue | (1 << 6);
- // 1110011
- System.out.println(Long.toBinaryString(longValue));
- String hex = Long.toBinaryString(longValue);
- // 1110011
- System.out.println(hex);
- // 115
- System.out.println(Integer.valueOf("1110011", 2));
- // 1110011
- System.out.println(Long.toBinaryString(longValue >> 0));
- // 1
- System.out.println(Long.toBinaryString(longValue >> 0 & 1));
- // 111001
- System.out.println(Long.toBinaryString(longValue >> 1));
- // 1
- System.out.println(Long.toBinaryString(longValue >> 1 & 1));
- // true
- System.out.println((longValue >> 0 & 1) == 1);
- // true
- System.out.println((longValue >> 1 & 1) == 1);
- // false
- System.out.println((longValue >> 2 & 1) == 1);
- // false
- System.out.println((longValue >> 3 & 1) == 1);
- // true
- System.out.println((longValue >> 4 & 1) == 1);
- // true
- System.out.println((longValue >> 5 & 1) == 1);
- // true
- System.out.println((longValue >> 6 & 1) == 1);
- // false
- System.out.println((longValue >> 7 & 1) == 1);
- // Demonstrate the bitwise logical operators.
- bitLogic();
- // Left shifting a byte value.
- byteShift();
- }
- /**
- * Left shifting a byte value.
- */
- private static void byteShift() {
- byte a = 64, b;
- int i;
- i = a << 2;
- b = (byte) (a << 2);
- // Original value of a: 64
- System.out.println("Original value of a: " + a);
- // i and b: 256 0
- System.out.println("i and b: " + i + " " + b);
- System.out.println(" ");
- }
- /**
- * Demonstrate the bitwise logical operators.
- */
- private static void bitLogic() {
- String binary[] = { "0000", "0001", "0010", "0011", "0100", "0101",
- "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101",
- "1110", "1111"
- };
- int a = 3; // 0 + 2 + 1 or 0011 in binary
- int b = 6; // 4 + 2 + 0 or 0110 in binary
- int c = a | b;
- int d = a & b;
- int e = a ^ b;
- int f = (~a & b) | (a & ~b);
- int g = ~a & 0x0f;
- // a = 0011 = 3
- System.out.println(" a = " + binary[a] + " = " + a);
- // b = 0110 = 6
- System.out.println(" b = " + binary[b] + " = " + b);
- // a|b = 0111 = 7
- System.out.println(" a|b = " + binary[c] + " = " + c);
- // a&b = 0010 = 2
- System.out.println(" a&b = " + binary[d] + " = " + d);
- // a^b = 0101 = 5
- System.out.println(" a^b = " + binary[e] + " = " + e);
- // ~a&b|a&~b = 0101 = 5
- System.out.println("~a&b|a&~b = " + binary[f] + " = " + f);
- // ~a = 1100 = 12
- System.out.println(" ~a = " + binary[g] + " = " + g);
- System.out.println(" ");
- }
- }