Java bit operation bitwise (bitwise) Operation bit
8 0000 0000 0000 1000 Original Code
1111 1111 1111 0111 anti-code
+ 1
1111 1111 1111 1000 (8 of the complement) to represent-8
-8 1111 1111 1111 1000 65528 complement (positive inverse code + 1)
65535 1111 1111) 1111 111165535
65535-65528=7+1=8
Operation Longvalue = Longvalue | (1 << N); You can set the value from the right count to the left number n + 1 bit to 1 in the Longvalue 2 binary representation, and do not affect the value above the other bits to do or |or operation with the 0 and 2 binary value variable x, without affecting the value of the 2 binary variable x
Operation Longvalue = Longvalue & ~ (1 << N); Can be in the Longvalue 2 binary representation, the number from the right to the left of the Nth + 1 bit set to 0, and does not affect the other bits above the value of 1 and 2 binary value variable x do with the &and operation, does not affect the value of the 2 binary variable x
Operation System.out.println ((Longvalue >> N & 1) = = 1); Can determine the value of the Longvalue 2 binary representation, from the right to the left of the Nth + 1 bit value is 0false or 1true
Java code
- 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);
- // One
- 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);
- //
- 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 = n, 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 ("\ r \ n");
- }
- /**
- * Demonstrate the bitwise logical operators.
- */
- private static void Bitlogic () {
- String binary[] = { "0000", "0001", "0010", "0011", "0100", "0101",
- "0110", "0111", "$", "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 =
- System.out.println ("~a =" + binary[g] + "=" + g);
- System.out.println ("\ r \ n");
- }
- }
Java bit operation bitwise (bitwise) Operation bit