Source code, reverse code, and supplementary code

Source: Internet
Author: User
Tags modulus

Directory

  • Introduction
  • Original code
  • Reverse code
  • Complement
  • Why is the 8-bit binary number in the range of-128 ~ + 127?
Introduction

The number of symbols in a computer has three Representation Methods:Original code,Reverse codeAndComplement. All three representation methods are available.Symbol bitAndNumeric bitIn the two parts, the symbol bits are expressed as "positive" with 0, and "negative" with 1, while the numerical bits are represented in different methods.

Original code

Original code(true formIs a binary point representation of numbers in a computer. The original code notation adds a sign bit (that is, the highest bit is the sign bit) before the value: positive, negative, and 1 (0 has two types of representation: + 0 and-0), and other bits indicate the value size.

Advantages:
Simple and intuitive. For example, we use 8-bit binary to represent a number. The original code of + 11 is 00001011, and the original code of-11 is 10001011.

Disadvantages:
The original Code cannot be directly involved in the operation and may cause errors.. For example, in mathematics, 1 + (-1) = 0 and 00000001 + 10000001 = 10000010 in binary are converted to-2 in decimal format. Apparently, an error occurred.

Reverse code

The anticode notation specifies that the anticode of a positive number is the same as the original code; the anticode of a negative number is the inverse of the original code by bit, except for the symbol bit.

Complement

The complement expression specifies that the positive value is the same as the original value. The negative value is added with 1 at the end of the reversed value.
In computer systems, all values are represented and stored using a complement code.. The reason is that you can use the complement code to process the symbols and numeric fields in a unified manner. addition and subtraction can also be processed in a unified manner. In addition, when the complement code and the original code are converted to each other, the calculation process is the same, and no additional hardware circuit is required.

Why is the 8-bit binary number in the range of-128 ~ + 127?

First, the eight-bit binary number is 0000-0000 ~ 1111 1111, which can represent 28 = 256 digits in total. If it represents an unsigned integer, it can represent 0 ~ 255. The calculation method is the conversion between binary and decimal.

If you want to represent a signed integer, use the first binary digit as the symbol bit, that is, 0 indicates a positive number, 1 indicates a negative number, and the last seven digits are numeric fields. This is the original code definition. In this way, there is no problem in real life, but there is a problem in the computer.

  1. Number representation

    In the original code, 0 represents two types (+ 0) 0000 0000 and (-0) 1000 0000, which leads to the uniqueness of the encoding ing, it is necessary to differentiate on the computer. However, + 0 and-0 have no practical significance.

  2. Number operation

    To solve the preceding number representation problem, we can forcibly set the 10000000 value after conversion to-128. However, another new problem is the number calculation. In mathematics, 1 + (-1) = 0, while 00000001 + 10000001 = 10000010 in binary, is converted to-2 in decimal format. Apparently, an error occurred. Therefore, the symbol bit of the original code cannot be directly involved in the operation. It must be separated from other BITs, which increases the hardware overhead and complexity.
    At this time, the Supplementary Code should be introduced. The expression of the Supplementary Code stipulates that the positive value is the same as the original code, and the negative value of the Supplementary Code is added with 1 at the end of the anticode. The anticode is defined as: the anticode of a positive number is the same as the original code; the anticode of a negative number is the inverse of its original code by bit, except for the symbol bit.

Question 1: Why do I need to introduce a supplementary code?

First, solve the first problem. The supplementary code is introduced to solve the problem of the number expression and number calculation in the computer. With the supplementary code, the symbol bit and the numeric field can be processed in a unified manner, that is, it refers to the automatic processing of the symbol bit in terms of the model operation in mathematics, and the natural processing of the symbol bit is realized by means of the automatic discard of the module, the expected requirements can be fulfilled without changing the physical architecture of the machine by changing the encoding.

Question 2: Why is the negative complement defined as the corresponding positive original code plus one?

To solve the second problem and understand the supplementary code, we must first understand the "model ".

The concept of Modulo helps you understand Population and complement code.

"Module" refers to the counting range of a metering system. Such as clock. A computer can also be seen as a metering machine. It also has a metering range, that is, there is a "module ". For example:
The Metering range of the clock is 0 ~ 11, modulo = 12. The N-bit computer metering range is 0 ~ 2 (n)-1, modulo = 2 (n ).

The "module" is essentially the amount of overflow produced by the metering device. Its value cannot be expressed on the metering device, and the metering device can only represent the remainder of the output model. Any metered tool with modulus can be reduced to addition.

For example, if the current hour hand points to 10, and the exact time is 6, you can adjust the time in either of the following ways: 10-4 = 6; the other is the dial for 8 hours: 10 + 8 = 12 + 6 = 6
In a 12-mode system, the addition of 8 is the same as the subtraction of 4. Therefore, any subtraction operation can be replaced by the addition of 8. For "modulo", 8 and 4 complement each other. In fact, in 12-mode systems, 11, and 6 all have this feature. The common feature is that the sum of the two equals the modulo.

Computers have the same concept and method. If n-bit computers are set to n = 8, if the symbol bit is not discussed, the maximum number that can be expressed is 11111111. If we add 1 to 100000000 (9 digits), because there are only 8 digits, the highest bit 1 is naturally lost. 00000000 is returned, so the modulo of the 8-bit binary system is 29. In such a system, the subtraction problem can also be converted into an addition problem. You only need to use the corresponding complement expression for the reduction. The complement number is used to process the computer logarithm, that is, the complement code.

The next step is to obtain the source of anti-increment:

For example,-8 can be regarded as 0-8, that is, 0000 0000-0000 1000, with a small reduction and a high-digit borrow, that is, 1 0000 0000-0000 1000 = 1111 1000, this can be seen as subtracting the number by using the modulus to obtain the population of the number. At the same time, the above formula can also be seen as (1111 1111-0000 1000) + 1, that is, 0000 1000 decimal addition 1.

Negative complement is defined as the inverse addition of the corresponding positive original code. The addition at the end of the negative number is the same effect. Check the anti-Code definition. Bitwise inversion is the bitwise inversion, including the symbol bit.

Calculate the inverse value of the original code of a positive number and obtain the complement code of the negative number corresponding to this positive number. Example ~ 6 =-7, and an octal complement code 1000 0000 will be added after the addition, And the complement Code corresponds to the original code 1000 0000, And the digit is also used as the symbol bit, that is,-128.

Based on the above content, we can explain the representation range of the Eight-bit binary number.

The complement range of an eight-bit binary positive number is 0000-0000 ~ 0111 1111 is 0 ~ 127. The complement range of negative numbers is positive. The source code is 0000 0000 ~ 0111 1111 take the inverse and add one (it can also be understood as a negative number of 1000 0000 ~ 1111 and 1111 are converted to the end of the anti-code plus one ).

So we get 0000 0000 ~ 1000 0001 1111 1111 as the complement code, the original code is 127 1111 (-1111), push forward in sequence, you can get the complement code of-1 is,Then, the source code of the complement code 0000 0000 is a 1000 0000 symbol bit, which can also be seen as a digit indicating-128., Which also explains why 127 (0111 1111) + 1 (0000 0001) =-128 (1000 0000 ).

Summary:In the computer, data is represented by a complement code, which unifies the operation of the symbol bit and the value bit, and solves the + 0 and-0 problems, the empty binary original code 1000 0000 is represented as-128, which also conforms to its own logic integrity. Therefore, the value range of the Eight-bit binary is-128 ~ + 127. During computation on a computer, all computation is performed by Code complement.

The essence of the computation inside the computer is the Code complement operation. The following example shows
Suppose a = 60, B = 13

The source code of A + BA is: 0011 1100 A's reverse code: 0011 1100a's complement: 0011 1100b's original code: 0000 1101 B's reverse code: 0000 1101b's complement: 0000 1101a + B 0011 1100 0000 1101 -------------- 0100 1001 0100 1001 convert to 10 hexadecimal integer is 73 A-B = a + (-B) A of the original code is: 0011 1100 A: 0011 1100a: 0011 1100a: 1000 1100-b: 1101 1111-B: 1111 0011a-B = 0011 1100 1111 0011 -------------- 0010 11110010 1111 is converted to a 10-digit integer, Which is 47. Note that if the result is a positive number, the complement code is the original code, however, if the result is a negative number, you need to convert the complement code-1 to the original code, and then convert it to the integer B-a with the original code 1011 1100-a as follows: 1100 1100-a complement: 0000 0100b original code: 1101 0000 B reverse code: 1101b complement: 0000 1101b-A = 1100 0100 0000 1101 -------------- 1101 0001 the result is a negative number according to the complement sign bit. Therefore, you must first convert-1 to the reverse code 1101 0000, and then the symbol bit remains unchanged, returns the original code 1010, which is-47.

Refer:
Why is the 8-bit binary number in the range of-128 ~ + 127?

Source code, reverse code, and supplementary code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.