Binary complement:
1, the internal computer system to store data in binary form.
2. The decimal data entered in the Java program is automatically converted to binary, and the Java internal is also binary for numeric operations, but the result is decimal.
The principle of binary complement:
positive + negative = modulo.
Modulo: The total number of data of a certain type, for example:
4-bit binary number modulo is 2^4=16
8-bit binary number modulo is 2^8=256
Negative number = modulo-positive number, which is the reason for the bitwise negation plus 1.
1, in the computer system, the values are used in twos complement to store.
2. The highest bit of binary is the sign bit, 0 is a positive number, and 1 indicates negative number.
3, positive value is its own, negative value is the highest bit (sign bit) unchanged, the other bit-by-bit inverse, plus 1. (The relationship between positive and negative numbers in a computer is reversed plus one)
4, two number add, if the highest bit (sign bit) has carry, then carry is discarded.
5. The complement operation is closed: The result of the operation is kept within the complement range, and the overrun range is overflow.
Example: (1101) 2
0010 bitwise inversion
0011 plus 1
(1101) 2=-3
The 4-bit twos complement can represent a maximum of 2^4 (16) and a range of -8~7
The 8-bit twos complement can represent a maximum of 2^8 (256) and a range of -128~127
?
Java Foundation twos complement