The original code, the inverted, complement adder

Undergraduate school too early original code, anti-code and complement, but did not practice too. Today in the brush 371 questions involving binary addition contains a negative number, this began to understand why the need to form a negative complement to stored in the computer.

Prior to this, a core concept must understand that congruence , Here is an example cited:

We have a wall clock, evenly distributed above the 12 hours, and now the clock point of time is 3:00, if we make the hour hand pointing to 5:00, you need to how to do it?

Easier to think of two kinds of practice:

  1. The hour counter dial 10 hours
  2. The hour hand clockwise dial 2 hours

This process is actually a modulo operation corresponds to:
(3-10) 12% 5 =
(3 + 2) = 12% 5

In this case, it can be said with -10 2 are congruent , i.e. when the mold is 12, a number corresponding to the process of +3 -10, 12 because beyond those portions have been required to process the remainder "erase" a.

Binary adder is based on this idea. Corresponding to the previous example, the computer 1Byte (8bit) as a basic unit of memory.
In a signed integer, the most significant bit is the sign bit, 8bit 7bit only to store data, so there 1Byte symbol may represent an integer in the range of -127 to 127 (i.e., 11111111 - 01111111). (However, we all know, generally indicated by said 1Byte signed integer in the range of -128 to 127, a given here refer to the article , since the content is not directed, and therefore not described herein.) Thus, the mold 12 corresponding to watch, where the mold is 128 (after 7 bit full 128 back to 0). Surely someone will ask, where 1 went into high, since only seven used to store values, so here produces an event called overflow , overflow 1 will be abandoned. When the watch clockwise as 14 hours, in fact, there once spilled, but we focus only time can show on the dial, so turn from 3:14 hours since 5:00. Computer memory overflow, in the embodiment corresponds to said primer "erase" process.

Based on the above analogy, we can draw rules:

3-10 so that the upper mold 12 is a timepiece, the equivalent of 3 + 2

Equivalent to

8bit memory cell in the mold 128, a number of A plus B a negative, corresponding to a number of A plus B and C with positive mode

Negative complement is the negative of the original code that corresponds to the original code with the remainder being, but we need to ignore the "sign bit" this number, because the nature of "sign bit" is not really an identification symbol, but for the final overflow the service.

For example, the original code is -3 1 000 0011. We all know it is inverted 1 111 1100 complement is 1 111 1101 complement the original code seen as a positive number, ignoring its sign, in fact, this number is 125, but its most significant bit is set to 1, as seen above, this is in addition to an overflow in.

For example, 20-3 = 17 is calculated, an addition with the complement -3, in fact, can be seen as 20 + 128 = 125% 17 (unless the sign bit on the line in the eyes):

0001 0100 + 1111 1101
=(1)0001 0001

1, the results obtained are discarded overflow 00,010,001. Notably, the value in the computer is stored in a 2's complement , binary addition is the complement arithmetic, the result is two's complement arithmetic. But in order to judge right and wrong, as our own, it needs to be converted to the original code to see the results. Since the result here is a positive number, the original code = complement, thus obtained result is 17. (If the converted step by step into the original code need to be negative readings)

Through the above presentation, I would like for programming Why would overflow values, we should also have some understanding - that is the result of the calculation can not be accommodated by the current memory unit of length, you must be bit more memory means to store the most significant bit carry like. Thus, when writing the code, to be sure the value is defined in accordance with the needs of the appropriate type, to ensure that it does not overflow.

Published 75 original articles · won praise 0 · Views 1514

Guess you like

Origin blog.csdn.net/qq_34087914/article/details/104095387