Bitwise operators with the use of Java in the skills summary

1. ( ^ )   exclusive OR operator

Operation rule: two operands bit XOR operation. [ Same takes 0 , the opposite takes 1] . I.e., two operands are the same, cancel each other .

skills:

(1) the value exchange. int a = 10; int b = 20 ===> a ^ b ^ a == b, a ^ b ^ b == a 

. 1   int F = 50 ;
 2   int G = 60 ;
 . 3  
. 4 F = F ^ G;
 . 5 G = F ^ G;
 . 6   F = F ^ G;
 . 7 System.out.println (F + "" + G);
 . 8  
. 9   The output is: 6050
View Code

(2) symmetrically offset transmissibility. Such as: int a = 10, int b = 20, int c = 30, int d = 40 ===> a ^ b ^ c ^ d ^ a == b ^ c ^ d, a ^ b ^ c ^ d. ^ a ^ b == c ^ d, a ^ b ^ c ^ d ^ a ^ b ^ c ^ d = 0.

2 may be used where the nature of which is to find a pair of values ​​in an array of an odd number of.

. 1  public  static  void main (String [] args) {
 2  
. 3          int Array [] = {2,3,4,4,3,5,5,6,6,7,7 };
 . 4  
. 5          int A = 0 ;
 . 6  
. 7          for ( int I = 0; I <be array.length; I ++ ) {
 . 8  
. 9              V ^ = Array [I];
 10  
. 11          }    
 12 is  
13 is          System.out.println ( "number that is not the same:" + V );
 14  
15      }
View Code

 

2. ( & ) Bitwise AND operator

Operation rule: 0 & 0 = 0; 0 & 1 = 0; 1 & 0 = 0; 1 & 1 = 1; namely: [ two at the same time is "1", the result was "1", 0 otherwise ]. Another: Negative Press complement form to participate in a bitwise AND operation.

skills:

(1) cleared. If you want to clear a unit, even if all its binary bit is 0, as long as a phase value you are zero, the result is zero.

1 public static void main(String[] args) {
2         int a=50;
3         int b=60;
4         System.out.println(a&b&0);//输出 0;
5     }
View Code

(2) takes a specified bit number

Method: find a number, the corresponding bit of X to be fetched, the corresponding bit number is 1, the remaining bits are zero, and this number X is "the operation" can be specified bit of X.

Example: Let X = 10101110,

    X out of the lower 4 bits, with X & 0000 1111 = 0000 1110 can be obtained;

    X may also be used to take the 2,4,6 position.

 1  public static void main(String[] args) {
 2         int a = 50;
 3         byte aBit = (byte) a;//00110010
 4         byte lastTwo = (byte) 3;//00000011
 5         byte lastFive = (byte) 31;//00011111
 6         byte result2 = (byte)(aBit & lastTwo);
 7         byte result5 = (byte)(aBit & lastFive);
 8 
 9         System.out.println (Byte.toString (result2)); // after take 2 50, the output --- 2 
10          System.out.println (Byte.toString (result5)); // after taking 5 50 bits --- 18 outputs 
. 11      }
View Code

 

3. ) bitwise OR operator

Operation rule: 0 | 0 = 0; 0 | 1 = 1; 1 | 0 = 1; 1 | 1 = 1; i.e.: two objects participate in operation as long as there is a 1, a value of 1 . Also, negative press complement form to participate in a bitwise OR.

Tips: The data for a certain location 1

For example: find a number, corresponding to the X set to 1, the corresponding bit number is 1, the remaining bits are zero. This number allows to X or X 1 in certain positions.

Example: 4 The low position of X = 10100000 1, with X | 0000 1111 = 1010 1111 can be obtained.

 

4. (-) non-operator

Operation rule: that is the negation operator NOT operation, variations in the binary 1 1 0,0 variants

That is 001010 1010 110101 non-operational after
the non-operation becomes int X    - (X + 1), i.e., = -6 ~ 5, ~ (-5) = 4

 

5. ( << ) left shift operator

Operation rule: The operand of each of all the bits to the left a number of bits (binary digits discarded left, the right complement 0).

Example: a = a << 2 a of the left two bits, a right 0s,

After left a a = a * 2; 

If left discarded do not contain a high level, each left one, the equivalent of the number multiplied by two .

 

6. ( >> ) right shift operator

All bits of each of a number of a number of bits to the right, the left complement positive numbers 0, 1 negative fill the left and right discarded.

Operand every right shift, equivalent to the number divided by 2 .

For example: a = a >> 2 a of the right two bits,

Left fill 0 or 1 up to look at is the number of shift is positive or negative.

 

7. ( >>> ) unsigned right shift operator

>>> operator the right to respective bits specified in expression2 expression1 bits. After the left to the right vacated bit positions are filled with zeroes. Right out bits are discarded.

For example: var temp = -14 >>> 2

The variable temp is 14 (i.e., binary 1,111,111,111,111,111 1,111,111,111,110,010), is equal to two to the right 1073741820 (i.e. binary 0,011,111,111,111,111 1,111,111,111,111,100).

 

Guess you like

Origin www.cnblogs.com/yuerugou54/p/12319286.html