Import? Java . io . *;
Public? Class? test
{
Public? Static electricity Invalid? main(String[]? Parameter)
{
int? a = 5;
system . out . println(~ a);
}
}
dtya@zzu:~$? javac? Test.java?
dtya@zzu:~$? java? Testing?
32 bits of type int in -6java, whose complement means 5:
0000 0000 0000 0000 0000 0000 ? 0000 0 10 1
The first 0 represents an integer.
Inversion:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ? 1 1 1 1 1 1 1 ? 1 1 1 1 10 10
The first bit 1 represents a negative number, and its value is the reciprocal of the last 3 1 bit plus 1.
Reverse first:
000 ? 0000? 0000 0000 0000 0000 0000 ? 0 10 1
Add 1:
000 ? 0000? 0000 0000 0000 0000 0000 ? 0 1 10(6)
Therefore: ~ 5 = 6
Appendix:
Add relevant knowledge:
There are three ways to represent the number of symbols in a computer, namely, original code, complement and complement. These three representations have two parts: sign bit and digital bit. The sign bit is "positive" of 0 and "negative" of 1, while the digital bit has different representations.
In computer systems, numerical values are always represented and stored by complement. The reason is that with the complement, the sign bit and numerical field can be handled uniformly; At the same time, addition and subtraction can also be handled in a unified way. In addition, the complementary code and the original code are converted to each other, and the operation process is the same, and no additional hardware circuit is needed.
positive number
The complement of a positive integer is the same as the original code.
The complement of example 1+9 is 000 100 1. (Note: This +9' s complement is represented by 8-bit binary, and there are many representations, including 16-bit binary, 32-bit binary and 64-bit binary. Each complement representation can only represent a finite number. )
negative number
In order to find the complement of a negative integer, the sign bit of the original code remains unchanged. First, subtract 1 from the original code, and finally, invert each number. (However, due to the particularity of binary, it is usually to reverse the digits of the numerical value first, and finally add 1 to the integer. )
The same number is different in different complement representations. For example, the complement of-15 is1110001in the 8-bit binary representation, but it is11655 in the 6-bit binary representation. All the following are expressed in 8-bit binary.
Example 2 Find the complement of -5.
Because the given number is negative, the sign bit is "1".
Last seven digits: original code-5 (100010/) → sign bit unchanged (10001kloc-0/) → inversion of numerical bit (1).
Therefore, the complement of -5 is111101.
Example 3 The complement representation of the number 0 is unique.
[+0] Complement =[+0] Inverse Code =[+0] Original Code =00000000
[-0] complement =11111+1= 0000000.
Convert to original code
Given the complement of a number, the operation of finding the original code is actually to find the complement again:
(1) If the sign bit of the complement is "0", it means that it is a positive number, and its original code is the complement.
⑵ If the sign bit of complement is "1", which means negative number, then finding the given complement is the required original code.
Example 4 If a complement is111001,the original code is100001(-7).
Because the sign bit is "1", that is to say, it is a negative number, so this bit remains unchanged and is still "1".
Other seven digits1111001are 000010;
Add 1, so it is100011.
Absolute value of complement
(called truth value)
The complement of example 5-65 is101111.
If1011111is directly converted into decimal, the result is not -65, but 19 1.
In fact, in a computer, if it is a binary number and its leftmost bit is 1, then we can determine that it is a negative number and express it with its complement.
If you want to get the true value of a negative two's complement, you can negate all the complements and add 1 to get its true value.
Such as: binary value:101111(the complement of -65).
Reverse: 0 1000000.
Add1:0100001(+65)