The most thorough understanding of using two's complement numbers

Let's start with the following snippet of code to illustrate the reasoning

The function of the above code is to enable the variable r_freq_cnt to change from 800 to +1 to 1100 gradually, and then to -1 to change to 800, repeating the cycle, similar to generating a triangular wave over time. The core is, how to make r_freq_cnt=r_freq_cnt+1 become r_freq_cnt=r_freq_cnt-1 when r_freq_cnt=1099? The core of understanding this problem is, how to understand numbers? Or how to understand the count in the register?

The process of register counting is that from 0 to the maximum value (there is no concept of signed and unsigned), an overflow occurs, the maximum value is added by 1, the counter returns to 0, and then counts again. This is the essence of counting. The size of the number we are concerned about is nothing more than paying attention to how many numbers are loaded in the container of the register. When we add a number (assuming there is no overflow), the number becomes larger. But if we add a relatively large number and cause the register to overflow, then the nature of the counter may make the number in the register smaller than the original. Take a simple example:

Assuming there is a 3-bit register, the maximum count is 7 (111), and adding 1 becomes 000;

       Suppose that the current number in the register is (101), which is 5. When adding 1, it becomes 110, which is 6. The number obtained at this time is larger than the original, that is, there is no overflow.

       Suppose, we now add 4, then according to the nature of the count, the value in the register is actually 001, which is 1. It is found that the number becomes smaller at this time. This actually reflects the essence of numbers - counting! !

       Then since the number can be reduced by addition, there is a kind of subtraction idea in it. So how much is added is just subtracted by 1. We find that just adding the size of the counter container, that is, the modulo value, just reduces by 1.

       For this, if 5 is to be subtracted from 1 to become 4, 7 must be added. So how to interpret this result. And we found that the fundamental idea of ​​​​complement numbers is actually the idea of ​​​​this kind of circular counting.

      Therefore, in the above program, subtracting 1 is actually adding the maximum value of the 24-bit register. This also gives us such a message that the two numbers participating in the operation must be of equal width to make sense.

     Essentially, when we understand the complement representation of negative numbers, we should not treat negative numbers as a kind of number, we should treat it as 0 minus this number, such as -3, it should be regarded as the process of 0-3! ! ! ! So how to implement this subtraction, of course, by adding a large number to overflow, which also led to the birth of complement code! ! !

     

Guess you like

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