Java Programming Basics--operator/original code inverse code complement "Knowledge System construction series" __ algorithm

Source: Internet
Author: User
Tags arithmetic operators bitwise logical operators numeric value
operator List Arithmetic Operators

+ addition-add operator on both sides of the value
-subtraction-left operand minus right operand
* Multiplication-multiply operator on both sides of the value
/division-left operand divided by right-hand operand
% modulo-right operand excluding left operand remainder
+ + self-increment-operand value increased by 1
-Decrement-operand value reduced by 1 relational operator

= = Check if the values of two operands are equal, if equal, the condition is true
!= Check if the values of two operands are equal, if the values are not equal, the condition is true
> Check the value of the left operand is greater than the value of the right operand, if so the condition is true
< Check the value of the left operand is less than the value of the right-hand operand, if so the condition is true
>= Check the value of the left operand is greater than or equal to the value of the right operand, if so the condition is true
<= Check if the value of the left operand is less than or equal to the value of the right operand, and if so the condition is the true operator

& Bitwise AND operator, which is only 1 if and only if one of the two operands is not 0
| Bitwise OR operator, as long as one of the two operands has a 0-time result, the bit is 1.
^ Bitwise XOR OR operator, two operands at a different time the result of that bit is 1
~ Each bit of the operand is flipped by a bitwise complement operator
<< bitwise left operator. Left operand specified number of digits left to right operand
>> bitwise RIGHT operator. Left operand the number of digits specified by the right operand of a bit
>>> bitwise right to complement the 0 operator. The value of the left-hand operand is moved to the right by the number of digits specified by the right-hand operand, and the resulting empty space is populated with 0 logical operators

&& is called logic and operator. If and only if the two operands are true, the condition is true
| | Called a logical OR operator. If any one of the two operands is true, the condition is true
! is called a logical non operator. Used to reverse the logical state of the operand. If the condition is true, the logical non-operator will get false. assignment operator

= Simple assignment operator that assigns the value of the right operand to the left-hand operand
+ + and assignment operator, which adds the left operand and the right-hand operand to the left operand
-= minus and assignment operator, which subtracts the left operand from the right operand and assigns it to the left operand
*= multiplication and assignment operator, which multiplies the left operand and the right-hand operand and assigns it to the left operand
/= and assignment operator, which divides the left operand from the right-hand operand to the left operand
%= modulo and assignment operator, which takes the left operand and the right-hand operand and assigns it to the left operand after modulo
<<= left Shift assignment operator
>>= Right Shift assignment operator
&= Bitwise AND Assignment operators
^= bitwise XOR or assignment operator
|= bitwise OR assignment operator other operators

Conditional operators (?:)
the concept of instanceof original code inverse code complement

The original code is the symbol bit plus the absolute value of the truth, that is, use the first digit to represent the symbol, the remaining bits represent values. For example, if it is 8-bit binary:

[+1] original = 0000 0001
[-1] original = 1000 0001

The first digit is the sign bit. Because the first bit is a sign bit, the range of 8-bit binary numbers is:
[1111 1111, 0111 1111] that is [-127, 127]
The original code is the easiest way for the human brain to understand and compute.

The way to represent the inverse code is:
The inverse code of a positive number is its own
The inverse of a negative number is based on its original code, the symbol bit is unchanged, and the remaining bits are reversed.

[+1] = [00000001] original = [00000001] Anti
[-1] = [10000001] original = [11111110] anti-

It is obvious that if an inverse code represents a negative number, the human brain cannot intuitively see its value. It is usually converted to the original code and recalculated.

The complement method is represented by:
The complement of a positive number is its own
The complement of a negative number is based on its original code, the symbol bit unchanged, the rest of you take the counter, the last +1. (i.e. on the basis of the inverse code +1)

[+1] = [00000001] original = [00000001] inverse = [00000001] Complement
[-1] = [10000001] original = [11111110] anti = [11111111] Complement

For negative numbers, the complement notation is also not intuitive to the human brain to see its value. It is usually also necessary to convert the original code to compute its numeric value. Why does the computer use the complement to calculate.

For positive numbers: [+1] = [00000001] original = [00000001] inverse = [00000001] Complement
For negative numbers: [-1] = [10000001] original = [11111110] inverse = [11111111] Complement

Computer identification "symbol bit" will obviously make the computer's basic circuit design becomes very complex! So people came up with a way to take the symbol bit into operation . As we know, subtracting a positive number from the algorithm equals adding a negative number, that is: 1-1 = 1 + (-1) = 0, so the machine can only add but not subtract, so the computer operation design is simpler.

Original code: 1-1=0,1-1 = 1 + (-1) = [00000001] Original + [10000001] original = [10000010] =-2, if the original code and let the sign bit added to the calculation, the result is incorrect

Anti-code: 1-1=0,1-1 = 1 + (-1) = [0000 0001] Original + [1000 0001] original = [0000 0001] Anti + [1111 1110] anti = [1111 1111] reverse = [1000 0000] =-0, send Inverse code is used to calculate subtraction, and the truth of the result is correct. And the only problem is actually in the "0" of this particular value. Although people understand that +0 and 0 are the same, 0 with symbols doesn't make any sense . And there will be [0000 0000] Original and [1000 0000] The original two encoding represents 0.

Complement, complement of the appearance, resolved 0 of the symbol and two coding problems: 1-1 = 1 + (-1) = [0000 0001] Original + [1000 0001] original = [0000 0001] Complement + [1111 1111] complement = [0000 0000] Complement =[0000 0000] The Original
Using the complement, not only fixes the 0 symbol and the existence of two coding problems , but also can represent a minimum number . This is why the 8-bit binary, which uses the original code or the inverse code to denote a range of [-127, +127], uses the complement representation of [-128, 127].

Because the machine uses the complement, the 32-bit int type that is commonly used in programming, such as Java, can represent a range of: [ -2^31, 2^31-1] because the first digit represents the sign bit. And the complement can be used to save a minimum value. Topic

Please write the results of the following expressions, which can be written in 10 or 16
1.0xaa | 0x55
2 & 240
3.10 ^ 12
4.-2 >> 1
5.-2 >>> 1

Answer:

Analysis: Hexadecimal number with 0x ... To indicate that the following hexadecimal bit is a four-bit, two hexadecimal bit is a byte, up to the following can have 8 hexadecimal digits, 32 bytes, such as: 0xFFFFFFFF. or ("| ") operation, all 0 is 0, the other is 1.
So: 0XAA is represented as 10101010 with binary notation, 0x55 is represented as 01010101, bitwise OR after 11111111, decimal number is 255, hexadecimal number is 0xFF.

Analysis: 10 into 2, with the number divided by 2, the logger and the remainder, the operator again divided by 2, the logger and the remainder ... Until up to 0 or the remainder is 0 stops, the remainder is composed of the binary from low to high (the last remainder is the binary lowest). and ("&") operation, all 1 is 1, the other is 0.
So: 15 equals 1111, 240 equals 11110000, 15 is padded with 0, and then 00001111, which is 00000000.

Analysis: Also or ("^") operation, the same take 0, different from 1.
So: 1010 ^ 1100 = 0110, decimal notation is 6, hexadecimal is represented as 0x06.

Analysis: Sign Right Shift (">>"), that is, the sign bit, negative sign bit 1, positive sign bit complement 0,-2 of the binary method is positive plus 1, so the 2 binary is represented by 0000 0000 0000 0000 0000 0000 0000 0010, take the reverse plus a For
1111 1111 1111 1111 1111 1111 1111 1110, that is, the binary representation of 2.
Note: >>, <<, >>>, operators only for int and long, byte, short, char type needs to be converted to int type in operation.
So: with the symbol right after the 1111 1111 1111 1111 1111 1111 1111 1111, in addition to the symbol bit, minus one reverse, get the signed decimal number of-1.

Analysis: Unsigned right-shift (">>>"), that is, regardless of positive numbers, the sign bits are 0 after the right shift.
So:-2 of the binary unsigned right one after the 0111 1111 1111 1111 1111 1111 1111 1111, that is, 2^31-1, two 31 Times Square minus one.
Note: The main difference between right and unsigned right shift is that the top of the left is up to 0 or up to 1, and the unsigned right shifts any time the highest digits are up to 0, and the symbol right shifts to positive 0, minus 1. (No unsigned left move.) )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.