AI For DIY

Monday, August 8, 2022

Complement and negative number

1. About complement

In order to perform arithmetic operations, it is necessary to be able to represent negative numbers even in the range of integers.

  The complement is a number that makes the sum of the digits of each digit a constant number. If it is 1's complement, it is a number that makes the sum of each digit 1, and if it is 10's complement, it is a number that makes the sum of the digits 10. The 10's complement for 4 is 7, the 10's complement for 4 is 6, and the 10's complement for 5 is 5. In decimal, there are 1 to 10's complement, in binary there are 1 and 2's complement.

 

2. Conversion to complement

In the case of the decimal system, calculation by complement is rarely performed, but only 10's complement is used in mental arithmetic or when using the abacus. However, since it is different from the writing method, practice is required.

  In the case of the binary system, 2's complement is required for calculation, and for this, 1's complement is obtained. Since 1's complement is expressed only as 1 or 0, if it is 1's complement, 0 is converted to 1 and 1 is converted to 0 so that the sum is 1, but it is the same as the number of digits before conversion. For example, 1's complement for 10011012 becomes 01100102, and 1's complement for 010011012 becomes 101100102.
  Next, 2's complement is converted so that the sum becomes 2, and it is difficult to convert immediately because rounding occurs at every digit, but it is easily solved by adding 1 to 1's complement. That is, 10011012 is converted to 01100102 (1's complement)
01100112 (2's complement), and 010011012 is converted to 101100102 (1's complement) 101100112 (2's complement).

 

Before conversion

1

0

0

1

1

0

1

1's complement

0

1

1

0

0

1

0

2's complement

0

1

1

0

0

1

1

 

Before conversion

0

1

0

0

1

1

0

1

1's complement

1

0

1

1

0

0

1

0

2's complement

1

0

1

1

0

0

1

1

 

To check for 2's complement, add the number before conversion and 2's complement and check that rounding occurs at every digit. That is, the sum of each digit becomes 2.

 

3. Handling negative numbers

Even if 0 and 1 recognized by the computer are repeatedly used, only positive numbers, which are numbers greater than or equal to 0, can be expressed, to express negative numbers, the largest digit is designated as the sign digit. Accordingly, even if the same number of digits is used, the range of the number to be expressed is halved, and even negative numbers can be written.
  If you write it based on the 8-digit binary number, it is as follows.

Positive (8 binary digits)

Negative (8 binary digits)

Decimal

Binary

Decimal

Binary

12710

011111112

110

111111112

12610

011111102

210

111111102

·

·

310

111111012

·

·

·

·

·

·

·

·

310

000000112

·

·

210

000000102

12610

100000102

110

000000012

12710

100000012

010

000000002

12810

100000002

 

With the 8th digit as the sign, 0 is a positive number, and 1 is a negative number. In the case of the next 7 digits, if the decimal number is converted to a binary number according to the existing method in the positive number section, it matches. In the case of a negative number interval, the decimal number is converted to binary number and then converted to 2's complement, it is match.
  For example, when converting decimal
12610 to binary, 1 is placed in the 8th digit, and the binary number of 12610 is 11111102, it converted to 00000012 (1's complement) 00000102 (2's complement), combined with results to match 100000102.
  If it is decided to use both positive and negative numbers for 8 binary digits in this way, it is possible to convert decimal numbers in the range of -12810 to +12710, if it is decided to use only positive numbers, it is possible to convert in the range of 010 to
25510.

 

4. Conclusion

Just learn how to calculate this yourself, and usually use a calculator on your computer or a converter on the Internet.

No comments:

Post a Comment