Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Coded representation of binary numbers

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Numerical information is represented by binary coding in the computer. Numbers can be divided into positive and negative. In general, "0" is used to indicate a positive sign, "1" is a negative sign, and the symbol bit is placed at the highest position of the number.

For example, 8-bit binary numbers A = (+ 1011011), B = (- 1011011), which can be represented in the machine as:

A:01011011

B:11011011

Several codes commonly used in computers-original code, inverse code and complement code.

1. Original code

The symbol bit is digitized to 0 or 1, and the absolute value of the number is encoded with the symbol, that is, the so-called "symbol-absolute value representation" code, which is called the original code.

For example:

Integer:

X = + 1011011, original code: [X] original = 01011011

X =-1011011, original code: [X] original = 11011011

Decimal:

For a signed pure decimal, its original representation is to use a place to the left of the decimal point as a symbol.

X = 0.1011, original code: [X] original = 0.1011

X =-0.1011, original code: [X] original = 1.1011

When the original code representation is used, the coding is simple and intuitive, and it is convenient to convert to the true value. But the original code also has some problems, one is that the representation of zero is not unique, because: [+ 0] = 000 0, [- 0] = 100 00 has ambiguity, which brings trouble to the machine to judge zero. Second, when the four operations are carried out with the original code, the symbol bits need to be processed separately, and the operation rules are complex. For example, in addition operation, if two numbers have the same sign and two numbers are added together, the result will be a common symbol; if two numbers have different signs, the large number will be subtracted from the decimal number, and the result will be marked with a large sign. In addition, the borrowing operation is very difficult if it is implemented with computer hardware.

two。 Inverse code

Inverse codes are rarely used, but as an intermediate code between coding and complementary codes, we still need to learn.

The inverse code of a positive number is the same as the original code.

The symbol bit of the negative inverse code is the same as the original code (still represented by 1), and the rest are reversed (0 to 1, 1 to 0). For example:

X = + 1100110, [X] original = 01100110, [X] inverse = + 1100110

X =-1100110, [X] original = 11100110, [X] inverse = 10011001

X = + 0000000, [X] original = 00000000, [X] inverse = 00000000

X =-0000000, [X] original = 10000000, [X] inverse = 111111111

Like the original code, the representation of zeros in the inverse code is not unique.

When X is a pure decimal, the inverse code is expressed as follows:

X = 0.1011, [X] original = 0.1011, [X] inverse = 0.1011

X =-0.1011, [X] original = 1.1011, [X] inverse = 1.0100

3. Complement code

(1) the concept of module

Modulus, in a physical sense, is the capacity of some kind of meter. For example, the module of the clock we use in our daily life is 12. The way to time a clock is to start from scratch when it reaches 12 (throw away a 12), which is mathematically a "modular (or remainder) operation (mod)". For example: 14 = 2.

If the exact time is 6 o'clock sharp and your watch points to 8 o'clock, how can you set the watch correctly? There are two ways: put the watch back 2 hours, or put the meter forward 10 hours, the effect is the same. That is:

8-2 = 6

(8 / 10) = 6

In the modular system, the above equation of 8-2 = 8-10 (mod 12) is true because 2 and 10 pairs of modulus 12 are complementary to each other (2-10 = 12). Therefore, it can be accepted that in a modular system, one number minus another number, or a number plus a negative number. Equal to the complement of the first number plus the second number: 8 + (- 2) = 8 × 10 (mod 12) We call 10 the "complement" of-2 under module 12. After the negative number is represented by the complement, the addition and subtraction can be unified as an addition operation. In a computer, the word length of data represented by a machine is fixed. For n-digits, the size of the module is: all n-digits are 1, and the last bit is added by 1. In fact, the value of the module has exceeded the range of numbers that the machine can represent, so the module cannot be expressed in the machine. If the result of the operation is greater than the module, the module is automatically discarded, which is equivalent to the realization of the modular operation. If there is an integer with n digits (including one symbolic bit), its modulus is 2 ^ n; if there is a decimal place and the place before the decimal point is a symbolic place, its modulus is 2. * * (2) complement representation * * from the above discussion, for a binary negative number, the complement can be obtained by adding its modulus and true value (modulo minus the absolute value of the number). Example: X =-0110 [X] complement = 2 ^ 4 + (- 0110) = 1010 X =-0.1011 [X] complement = 2 + (- 0.1011) = 1.0101 because there is no truth form of numbers in the machine, it is not easy to use the above formula to calculate the complement code in the machine. but a simple method can be deduced from the above formula. "for a negative number, the complement is obtained by adding 1 to the last bit of the inverse code of the number." "for positive numbers, the original code, inverse code, and complement form are the same." One of the characteristics of the complement is that zero represents the only one. [+ 0] complement = 0 000 [- 0] complement = 11 1 + 1 = 1 000 | _ | | N-bit n-bit | n-bit | _ automatic loss is a simple method of finding complementary codes. It is often referred to as "plus 1" for short.

(3) the operation rules of complement

Another advantage of using complement is that it is most convenient to use complement when numerical information is involved in arithmetic operation. First of all, "the symbol bit can be used as a numerical value to participate in the operation," finally, the correct result symbol can still be obtained, and the symbol does not need to be processed separately; secondly, when the complement code is used for operation, the subtraction operation can be converted into addition operation, which simplifies the operation circuit in the hardware.

For example:

Calculate 67-10?

[+ 67] original = 01000011, [+ 67] complement = [+ 67] original

[- 10] original = 10001010, [- 10] complement = 11110110

0 10 000 1 1 [+ 67] complement + 1 1 1 10 1 10 [- 10] complement-100 1 1 100 1 = 57 | _ _ carry of the highest digit Naturally lost because the word length is only 8 bits. Therefore, the carry of the highest bit of addition is naturally lost, achieving the modular effect (that is, losing a modulus). It should be noted that "the result of the complement operation is still the complement." In the above example, we know from the result symbol bit that the result is positive, so the complement is the original code, converted to a decimal number of 57. If the result is negative, it is the complement form of a negative number. If you want to become the original code, you need to complement the complement code, which can be restored to the original code. For example: 10-67 =? [+ 10] original = 00001010 = [+ 10] complement [- 67] original = 11000011 [- 67] complement = 10111101 00001010 + 10111101-11000111 [result] complement = 11000111 [results] the original = 10111001, so the true value of the result is-0111001 and the decimal system is-57. Can the above two examples show that the result of complement operation is always correct? Take a look at the following example: for example: 85 + 44 =? From the symbol bits of the result, it can be seen that the result is a negative number. But the addition of two integers cannot be negative. What is the problem? It turns out that this is caused by an "overflow", that is, the result is beyond the range of numbers that can be represented by a certain number of binary numbers. The above is an excerpt from C++ language programming (4th Edition), Tsinghua University Press.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report