1.Original code
The highest bit is used as the sign bit (0 represents positive, 1 represents negative), and the remaining bits represent the absolute value of the value itself (expressed in binary).
For simplicity, we use 1 byte to represent an integer.
The original code of +7 is: 00000111
The original code of -7 is: 10000111
2.Reverse code
If a number is positive, its complement is the same as the original code; if a number is negative, the sign bit is 1, and the remaining bits are the inversion of the original code.
For simplicity, we use 1 byte to represent an integer:
The complement of +7 is: 00000111
The complement of -7 is: 11111000
3. Complement code
Complement code: If a number is positive, its original code, complement code, and complement are the same; if a number is negative, the sign bit is 1, and the remaining bits are the inversion of the original code, and then the entire number is added by 1. For simplicity, we use 1 byte to represent an integer:
The complement of +7 is: 00000111
The complement of -7 is: 11111001
Given the complement of a negative number, convert it to a decimal number. Steps:
1. First, I will contradict you;
2. Convert it to decimal number;
3. Add the negative sign and subtract 1.
For example:
11111010, the highest bit is 1, which is a negative number. First negate each bit to get 00000101, convert it to a decimal number to get 5, add a negative sign to get -5, and then subtract 1 to get -6.
Why is it a negative number when I cast int a=232; to byte type? ? ?