note
- The following conclusions are examples of variables with a length of 8bit.
Original code
The original code is the absolute value of the sign bit plus the true value, that is, the first bit is used to represent the sign, and the remaining bits are used to represent the value. For example, if it is an 8-bit binary:
[+1] = [00000001] original
[-1] = [10000001] original
[+0] = [00000000] original
[-0] = [10000000] original
The first bit is the sign bit. Because the first bit is the sign bit, the range of 8-bit binary numbers is:
[1111 1111 , 0111 1111]
That is, the range of values: [− 2 n − 1 − 1, 2 n − 1 − 1] [-2^{n-1}-1, 2^{n-1}-1][−2n−1−1,2n−1−1 ] , for 8bit variable[-127, 127]
where, n-number of bits is variable.
Complement
The representation of the inverse code is:
- The complement of a positive number is itself
- Inverted negative which is
原码
based on the sign bit the same, each of the remaining bit is inverted.
[+1] = [00000001] Original = [00000001] Anti
[-1] = [10000001] Original = [11111110] Anti
[+0] = [00000000] Original = [00000000] Anti
[-0] = [10000000] Original = [11111111] anti
Value range: [− 2 n − 1 − 1, 2 n − 1 − 1] [-2^{n-1}-1, 2^{n-1}-1][−2n−1−1,2n−1−1 ] , for 8bit variables[-127, 127]
Complement
The representation method of complement is:
- The complement of a positive number is itself
- Negative complement is, in its
原码
basis on the sign bit the same, the remaining bits inverted, and finally +1. (I.e., on the basis of the inverted +1)
[+1] = [00000001] Original = [00000001] Inverse = [00000001] Complement
[-1] = [10000001] Original = [11111110] Inverse = [11111111] Complement
[+0] = [00000000] Original = [00000000 ] Inverse = [00000000] Complement
[-0] = [10000000] Original = [11111111] Inverse = [00000000] Complement
Value range: [− 2 n − 1, 2 n − 1 − 1] [-2^{n-1}, 2^{n-1}-1][−2n−1,2n−1−1 ] , for 8bit variables[-128, 127]
Conversion relationship between positive and negative complements
N = number corresponding to the complement of the complement of the negative 按位取反
post +1
.
= Negative complement positive number corresponding to complement 按位取反
post +1
.
[+1] = -[-1] = ~[-1] complement + 1 = ~[11111111] complement + 1 = [00000000] complement + 1 = [00000001] complement
[-1] = -[+1] = ~[+1] complement + 1 = ~[00000001] complement + 1 = [11111110] complement + 1 = [11111111] complement