[Java learning] Operators in Java

Both Java and C++ are divided into the following types of operators: arithmetic operators, relational operators, logical operators, bitwise operators, etc. Next, I will briefly explain these operators in Java.

One, arithmetic operators

The arithmetic operators we commonly use are as follows:

Operator description
+ Addition operation
- Subtraction operation
* Multiplication
/ Division operation
% Remainder operation: the remainder of the left operand divided by the right operand
++ Increment 1 operation
Decrement 1 operation

Here +, -, * are exactly the same as our mathematical operation rules, so I won't explain them.
% Is generally used for integer remainder operations, sometimes called modulo.
When the left and right operands of / are integers, it means integer division (the result is only the integer part), otherwise it means floating-point division.
It should be noted that dividing an integer by 0 will generate an exception, while dividing a floating-point number by 0 will result in a result (infinity or NaN)
Insert picture description here

Binary arithmetic operator += -= %=

We can also write binary arithmetic operators in a simplified format in assignment statements.

For example, x+=4 is equivalent to x=x+4.
We usually put arithmetic operators on the left side of the assignment symbol for splicing (*=, /=, %=, +=. -=)
Insert picture description here

Increment/decrement operator ++--

In program programming, adding 1 and subtracting 1 are the most common operations for numeric variables. Java also learns from the implementation of C++, using the operator of increment ++ and decrement - to realize the operation of variable addition and subtraction by 1.
For example: int n=3;n++
then the value of n will become 4. Because the operator changes the value of the variable, the operand here cannot be a numeric value. So 4++ is an illegal statement.
In fact, self-increment ++ and self-decrement-- are divided into two cases:
prefix form: ++n;--n (expression uses the changed value)
postfix form: n++; n--(expression uses the value before the change )
These two forms make the value of the variable itself change the same, but the expressions using them will produce different results. The difference can be clearly seen in the following example.
Insert picture description here
Although I understand the difference, I don't recommend using the increment/decrement operators in other expressions. This will not only make the code difficult to understand, but may also cause other bugs.

Two, relational operators

Insert picture description here
Here == is used to determine whether the two sides of the operator are equal, and should be distinguished from the assignment operator =. There are only two types of return results for relational operators, true/false , and assignment operators are different.
Insert picture description here

Logical operator && ||!

In addition, Java follows the habit of C++ and also uses logical operators: AND, OR, and NOT. And use the "short-circuit" method of evaluation: if the first operand can already determine the value of the entire expression, then the second operand does not need to be calculated.
Insert picture description here
"Short-circuit" methods such as && operation, if the left operand is false, then the right operand will not continue to be calculated.
For the following example, a/0 does not report an error (under normal circumstances-the divisor cannot be 0, otherwise the program reports an error).
Insert picture description here
"Short-circuit" methods such as || operation, if the left operand is true, then the right operand will not continue to be calculated.
! Not arithmetic, everyone is familiar with it, so there is no need to talk about it.

Ternary operator? :

What does the ternary operator mean? : Means when? The first expression is evaluated when the condition of is true, otherwise the second expression is evaluated.
For example, the expression return x<y?x:y; means returning the smaller value of x and y.
In many cases, this operator simplifies our program and is a very useful operator.
Insert picture description here

Bit operator

Insert picture description here
The bitwise AND & or | operation is very simple, the bit here refers to the binary bit, so we can get the result as long as the value is expressed in binary.
The specific example is as follows: The
Insert picture description here
calculation process is as follows:
Insert picture description here

Not operator ~

~ Operation rules: If the bit is 0, the result is 1, if the bit is 1, the result is 0. The non-operator is a unary operator.
Specific example: a=2, ~a=-3 (the specific calculation involves the knowledge points of the complement code, if you don’t understand it, you can understand the complement code) The
Insert picture description here
calculation process is as follows:
here is a brief explanation : the original code of the positive number, the inverse code, the complement The code is the same; the inverse code of a negative number is the inversion of the original code except for the sign bit (the highest bit), and the complement code is the inverse code +1. For negative numbers, the computer stores all complements, so we need to calculate the source code of this negative number, that is, the source code is equal to (the sign bit remains unchanged) the complement minus and then inverted.
Insert picture description here

XOR operator ^

^Operation rules: If the bits of the two operands are the same, the result is 0, and if they are different, the result is 1. The XOR operator is a binary operator.
The specific example is as follows: the
Insert picture description here
calculation process is relatively simple:
Insert picture description here

Shift operator >> shift right << shift left

<<     :     左移运算符,num << 1,相当于num乘以2
>>     :     右移运算符,num >> 1,相当于num除以2
>>>    :     无符号右移,忽略符号位,空位都以0补齐

The specific example is: what
needs to be explained here is that when a positive number is output in binary representation, the 0 on the left is not output; when a negative number is output in binary digits, the complete 32-bit output is required. The
Insert picture description here
calculation process is as follows:
When paying attention to the shift operator The parameters on the right need to be operated modulo 32. That is, the calculation results of 1<<35 and 1<<3 are the same.
Insert picture description here

The practical application of bitwise operators

Bitwise operators are faster than addition and subtraction operators. Generally, bitwise operators are used for bitwise operations.
Especially the operations of 2,/2 can be replaced with << and >>.
Operator precedence :
suffix (()[].)> unary (++--! ~)> multiplicative (
/%)> additive (+ -)> displacement (>> << >>>)> Relationship (> >= <<=)> Equal (== !=)> &> ^> |> ?:> Assignment (=, += etc.)> comma

Count the number of ones in the binary representation of an integer

Topic: Input an integer and output the number of 1 in the binary representation of the number. For example, the binary representation of 9 is 1001, and 2 bits are 1, so if you input 9, the function outputs 2.

The first idea : use the >> operator to shift the data to the right, and continue to do the AND operation with the number 1. If the result of the AND operation is 1, it means that the position is 1, number++; otherwise, it is 0, continue Judge the next one.
Insert picture description here
Careful readers may find that the right shift in the code is >>> instead of >>. Because when I was writing the code, it suddenly occurred to me that if n is a negative number here, if you use >> to shift to the right, the leftmost complement will be 1, and if you keep moving to the right, you will always fill in 1 will cause an endless loop (1000) >> (1100 )>>(1110)>>(1111)>>...>>1111. Using >>> will not cause problems, because at this time, no matter whether n is a positive or negative number, 0 will be added in the right shift operation, and the while will always stop.

Idea 2 : For any data n, the n-1 operation is to change the rightmost 1 to 0 in the binary representation of n, and all the back parts of the rightmost 1 are reversed. n=n & (n-1), then in the binary representation of n, the rightmost 1 becomes 0. Use this feature to count the number of ones in the binary representation.
Insert picture description here

Determine whether an integer is an integer power of 2

Insert picture description here

Determine the two integers m and n, calculate how many bits in the binary representation of m need to be changed to get n

Insert picture description here

Do not use addition, subtraction, multiplication and division, complete the addition operation of two numbers

Insert picture description here

Guess you like

Origin blog.csdn.net/ly_6699/article/details/113737945