Why use one's complement to represent negative numbers, and why are negative numbers 1 more than positive numbers?

(1) Unsigned 16-bit range

0000 0000 0000 0000 to 1111 1111 1111 1111

The value range of unsigned 16-bit integer data is 0 to 65535.

(2) Signed 16-bit range

Positive numbers: 0000 0000 0000 0001 to 0111 1111 1111 1111

The decimal number is 1 to 32767

Negative numbers 1000 0000 0000 0000 to 1111 1111 1111 1111

The decimal number is -32768 to 32767

(3) Complement of -1 and -32768

The original code of 1: 0000 0000 0000 0001;

Reverse: 1111 1111 1111 1110;

+1: 1111 1111 1111 1111。

The complement of -1 is 1111 1111 1111 1111.

 

The original code of 32768: 1000 0000 0000 0000

Negation: 0111 1111 1111 1111

+1: 1000 0000 0000 0000

 

(4)-32769 Why not work

 32769 Original code: 1000 0000 0000 0001

Invert: 0111 1111 1111 1110

+1: 0111 1111 1111 1111

What is this, 32767

(5) Why use complements to display negative numbers

For easy operation when adding positive and negative numbers

For example, 2+(-1), directly displayed with a negative number, is -3

Use one's complement

(6) Why is there 1 more signed negative number than positive number?

If it is 8-bit signed

Positive number range 0000 0001 ~ 0111 1111 (1~127)

Negative number range 1000 0001 ~ 1111 1111 (-127~1)

In addition, the first digits are 0 and 1, respectively, and the following combinations

0000 0000 and 1111 1111
are 0 and -128 respectively

Originally, the first digit is 0 or 1 (positive and negative signs) and the following 7 digits, the corresponding number of combinations is the same. 

But 0000 0000 is used for 0, and 1111 1111 is used for negative numbers, so negative numbers are one more than positive numbers. 

Guess you like

Origin blog.csdn.net/liyang_nash/article/details/103972284