Java SE(25) operator-bit operator

Bit operator

The bit operator is a symbol for performing operations on each bit of the binary , and it is specifically for new operations on 0 and 1.

Operator Calculation example result
&

      Bitwise and

 (All are 1 to 1)

0&0 0
0&1 0
1&0 0
1&1 1
|

      Bitwise or

   (1 is 1)

0|0 0
0|1 1
1|0 1
1|1 1
^

     Bitwise XOR

  (The difference is 1)

0^0 0
0^1 1
1^0 1
1^1 0
~ Negate ~0 1
~1 0
<< Shift left (Positive number) 0 000 0010 << 2 0000 1000
(Negative number) 1 001 0011<<2 0100 1100
>> Shift right 0110 0010>>2 0001 1000
1110 0010>>2 1111 1000
>>> Unsigned right shift 0110 0010>>>2 0001 1000
11...1110 0010>>>2 0011...11 1000

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

table of Contents

Bit operator

Bit operator &

Bit operator|

Bitwise operator^

Bitwise operator~

Bit operator <<

Bitwise operator >>-signed right shift

Bitwise operator >>>-unsigned right shift


Bit operator &

The bit operator & is to perform an AND operation on the two binary numbers involved in the operation . If both binary bits are 1, the result of the bit operation is 1, otherwise it is 0.

Example:     

Bit operator|

The bit operator & is to perform an OR operation on two binary numbers involved in the operation . If one of the binary digits has a value of 1, the result of the bit is 1, otherwise it is 0.

Example:

Bitwise operator^

The bit operator ^ is to perform XOR operation on two binary numbers involved in the operation . If the binary bits are the same, the value is 0, otherwise it is 1.

Example:

Bitwise operator~

Bit operator ~ only operates on one operand. If the binary bit is 0, the inverse value is 1; if the binary bit is 1, the inverse value is 0.

Example:

Bit operator <<

The bit operator << is to move all binary bits of the operand one bit to the left . During operation, the space on the right is filled with 0 , and the left part is discarded .

Example:

Interview questions: The fastest way to 4x8-4<<3.

Bitwise operator >>-signed right shift

The bit operator >> is to move all binary bits of the operand one bit to the right . In operation, when the left side of the slot depending on the sign bit of the original 1 or the complement 0 (the original is negative to make 1 is a positive number would complement 0 ).

Example:

Bitwise operator >>>-unsigned right shift

The bit operator >>> moves all the binary bits of the operand one bit to the right . During operation, the left space is filled with 0 (the positive and negative numbers of the original number are not considered).

Example:

public class FuShu {
	public static void main(String[] args) {
		System.out.println(Integer.toBinaryString(-6));
		
		int m=-6;
		m=m>>>2;
		System.out.println(Integer.toBinaryString(m));
		System.out.println(m);
	}
}

operation result:

 

 

 

Guess you like

Origin blog.csdn.net/wqh101121/article/details/112601948