binary representation of negative numbers

First understand the following concepts
Source code:

正数原码就是最简单的二进制表示,比如5 就是0000 0101 有八位的话
负数原码 -5 就是有最左边以为是符号位 1000 0101

If the original code of the negative number is used to represent the buye, it can only represent the number between -127 and 127, because there are two representation methods for 0, 0000 0000 and 1000 0000, which leads to a problem, and can only represent 255 numbers. Even considering 1000 0000 to represent -128, another problem arises, the sum of +1(0000 0001)-1(1000 0001) is (1000 0010) not 0, and the result is -2.
How to represent -128~127 numbers, and in line with addition and subtraction logic.
Use negative numbers' complement.
Introduce the definition of inverse code.
The original code, inverse code and complement code of positive number and 0 are the same.
The complement of a negative number is the inversion of the other digits except the sign bit, and the complement is the complement of the number plus 1. Such as
-1 original code 1000 0001, complement 1111 1110, complement 1111 1111
-2 original code 1000 0010 complement 1111 1101, complement 1111 1110
-3 original code 1000 0011 complement 1111 1100, complement 1111 1101
-1 + -2 = -3 is equivalent to adding directly in binary, discarding the highest bit.
-126's original code 1111 1110's complement 1000 0001, complement 1000 0010
-127's original code 1111 1111's complement 1000 0000, complement 1000 0001
-128's complement is 1000 0000, for 8 bits, it has no complement and original code.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325855443&siteId=291194637