One or two binary (original code, anti-code, complement)
- The highest bit of binary is the sign bit ("0" represents a positive number, "1" represents a negative number);
- There are no unsigned numbers in Java ;
- The computer is computed with the complement of integers;
1. Original code: Converts an integer to a binary representation
In the case of type int, theint type is 4 bytes and a total of 32 bits .
For example, 2 of the original code is:00000000 00000000 00000000 00000010
-2 of the original code is:10000000 00000000 00000000 00000010
2. Anti-code
Inverse code for positive numbers: same as original code
Negative anti-code: The original code of the symbol bit unchanged, the other bits take the reverse
For example, the counter code of 2 is:11111111 11111111 11111111 11111101
3. Complement
The complement of positive numbers: Same as the original code
Complement of negative numbers: Anti-code +1
For example, the complement of 2 is:01111111 11111111 11111111 11111110
Second, bitwise operations
There are 4 bitwise operators in Java:
1.bitwise AND &:Two bits are 1 and the result is 1
For example, 2&3 = 2
2 The original code is: 00000000 00000000 00000000 00000010
3 The original code is: 00000000 00000000 00000000 00000011
2&3 The original code: 00000000 00000000 00000000 00000010 = 2
2.bitwise OR |:At least one bit is 1 and the result is 1
For example, 2|3 = 3
2 The original code is: 00000000 00000000 00000000 00000010
3 The original code is: 00000000 00000000 00000000 00000011
2|3 The original code: 00000000 00000000 00000000 00000011 = 3
3.Bitwise XOR or ^:Two bits one is 1, one is 0, and the result is 1
For example, 2|3 = 3
2 The original code is: 00000000 00000000 00000000 00000010
3 The original code is: 00000000 00000000 00000000 00000011
2^3 The original code: 00000000 00000000 00000000 00000001 = 1
4.Bitwise Counter ~:0 becomes 1, 1 becomes 0
For example, = 3
Reverse 2 of the original code: 11111111 11111111 11111111 11111101 (the complement of the reversed result, that is, the complement of 3.) We need to release the original code from the complement to get-3)
Convert to Counter code: 11111111 11111111 11111111 11111100 (complement minus 1)
Converted to original code: 10000000 00000000 00000000 00000011 =-3 (the symbol is the same, the other bits take the reverse)
Summarize
- Positive numbers of the original code, anti-code, and complement are the same;
- Negative number of the inverse code = The original code of the symbol bit unchanged, the other bits take the reverse;
- The complement of negative number = inverse code +1;
- 0 of the original code, anti-code, complement is 0;
- The computer is operated in the complement;
- The inverse is different from the inverse code;
2018-01-06 18:38:29
Java: Binary (original code, inverse code, complement) and bitwise operations