java operator

 operator classification

         Operators specify how to operate on operands. There are many Java operators that make up expressions. Operators are divided according to the number of operands they require, and there can be unary operations, binary operators and ternary operators. Operators are classified according to their functions, including arithmetic operators, assignment operators, relational operators, logical operators, bitwise operators and other operators.

   arithmetic

     Unary operation + (positive) - (negative) ++ (increment) - - (decrement)

     Binocular operation + - × / % modulo

     Trinary operation: a>b ? true : false Returns true when a>b, otherwise returns false

   relation

     Equal sign: ==, not equal sign: != , greater than sign: >, less than sign: <, greater than or equal sign: >= , less than or equal sign: <= 

    bit and logic

          Bitwise operators AND (&), NOT (~), OR (|), XOR (^)

     assignment operator

            =    +=     -=    *=    /=    %=    &=   ^=    |=   <<=    >>=

     Displacement can only handle positive integer operators, char, short, byte will be converted to int type when performing displacement operation, and the operation result is also int type

     <<Signed left shift>>Signed right shift>>>Unsigned right shift 

 operator demo

      And ( & ) performs bitwise operations when both sides of & are numbers, and performs logical judgment when both sides of & are boolean.

     Difference between & and && 

 

  int a =1 , b = 2 ,c = 3;
  a == b && b == c //When a != b, return false directly without b== c judgment
  a == b & b == c //When a!=b, the judgment of b ==c is also performed

     & to do number crunching

     3&6  = 2

     0011 // binary result of 3

     0110 // binary result of 6

     0010 // The calculation result is 1 when the upper and lower two digits are 1 at the same time

     OR (|) Performs a bitwise operation when both sides of | are numbers, and performs logical judgment when both sides of | are boolean. The difference between | and || is the same as above

     | Do number operations

      3|6  = 7

     0011 // binary result of 3

     0110 // binary result of 6

     0111 // The result of the calculation is 1 when one of the upper and lower two digits is 1

    not (~) ~3 = -4   

     0000 0011 // binary result of 3

     1111 1100 // Negative if it is 0, the result is 1, if it is 1, the result is 0 

     0000 0100 //The high bit is equal to 1, so the original code is equal to the negative number, the negative number is inverted and the high bit remains unchanged, and 1 is added at the end

    Different (^)   

     6^3  =  5

     0011 // binary result of 3

     0110 // binary result of 6

     0101 // same result is 0, different result is 1   

     << signed left shift     

     3 << 2  = 12 

     0011 // binary result of 3 

     1100 //result after moving two places to the right

     << Signed right shift

     3 >> 2 == 0

     0011 // binary result of 3 

     0000 //result after moving two digits to the right

  The displacement of integers is relatively simple. Let's take a look at the displacement of negative numbers. Negative numbers are bitwise inverted before shifting, and the shifting operation is performed after inversion. The secret of negative displacement: when a negative displacement is performed, first invert the binary code and add one, and then perform the displacement without changing the sign bit. If you move to the left, then add zeros to the back, and if you move to the right, add 1 to the front, and then invert and add by bit. one. Integer negation is itself, negative number negation sign bit remains unchanged, other books are negated

    -3  >> 2  = -1

    1000 0011 // binary original code of -3

    1111 1101 // -3's complement plus one

    1111 1111 // The result after shifting, the sign bit is unchanged and shifted to the right, its front is complemented by 1

    1000 0001 //The negative sign bit remains unchanged, after adding one, the binary code, 

    -3 >>> 2 = 1073741823 unsigned displacement  

    1000 0000 0000 0000 0000 0000 0000 0011 //-3 four-byte binary original code

    1111 1111 1111 1111 1111 1111 1111 1101 //-3's complement

    0011 1111 1111 1111 1111 1111 1111 1111 //It must be an integer after unsigned displacement, so no need to transcode, directly convert to decimal, the result is 1073741823

  

    Difference between a+b and a+=b

short a = 4 ;
 int  b = 5;
 a+=b ; //It can be compiled successfully, and high-precision type conversion will be automatically performed when calculating
 a= a+b ; //The type is inconsistent, an error will be reported

 

 

    

    

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326536130&siteId=291194637