Binary one's complement sum operation

Calculation of the checksum of UDP protocol .

  In fact, this calculation principle is not difficult in essence, it is a binary one's complement summation operation, specifically:

                                          0+0=0;0+0=0

                                          1+0=0+1=1;1+0=0+1=1

                                          1+1=10;1+1=10

  Among them, the 1 in 10 is added to the next column. If it is 1+1 in the highest column, then the obtained 10 is left with 0, and 1 is moved to the lowest column, and then a binary addition is performed with the lowest bit.

  In the book of Teacher Xie, I talked about how a 15-byte UDP data is checked on the sender, and then a binary vertical form is listed and a result is given, but it does not describe how it is. Calculated. After my continuous efforts, I finally wrote the entire binary calculation process. When I was in class at the Academy of Mathematical Sciences, the teacher talked about a simpler hexadecimal calculation method, which is indeed much simpler in comparison. Okay, let's take the picture first.

The title given by Teacher Xie in the textbook is like this.

二进制版
1001 1001 0001 0011 //伪首部源IP地址前16位
0000 1000 0110 1000 //伪首部源IP地址后16位
1010 1011 0000 0011 //伪首部目的IP地址前16位
0000 1110 0000 1011 //伪首部目的IP地址后16位
0000 0000 0001 0001 //伪首部UDP协议字段代表号17,前面8位是填充0
0000 0000 0000 1111 //伪首部UDP长度字段
0000 0100 0011 1111 //UDP头部源IP地址对应的进程端口号
0000 0000 0000 1101 //UDP头部目的IP地址对应的进程端口号
0000 0000 0000 1111 //UDP头部UDP长度字段
0000 0000 0000 0000 //UDP头部UDP检验和
0101 0100 0100 0101 //数据字段
0101 0011 0101 0100 //数据字段
0100 1001 0100 1110 //数据字段
0100 0111 0000 0000 //数据字段+填充0字段
    
十六进制版
9913        //伪首部源IP地址前16位
0868        //伪首部源IP地址后16位
AB03        //伪首部目的IP地址前16位
0E0B        //伪首部目的IP地址后16位
0011        //伪首部UDP协议字段代表号17,前面8位是填充0
000F        //伪首部UDP长度字段
043F        //UDP头部源IP地址对应的进程端口号
000D        //UDP头部目的IP地址对应的进程端口号
000F        //UDP头部UDP长度字段
0000        //UDP头部UDP检验和
5445        //数据字段
5354        //数据字段
494E        //数据字段
4700        //数据字段+填充0字段

 In the binary version, it is not possible to do vertical addition directly from the first column on the right . Not to mention the vertical addition in decimal system, the vertical addition in binary system cannot be done.

      The correct approach is:
      (1) Let the first row and the second row do the binary complement operation.
      According to the above rules, when 1+1=10 is encountered, write a small 1 under the left adjacent column (similar to the previous decimal carry addition), and then perform the binary complement operation on the side column to obtain a number, if there is no carry , Continue to do the binary complement operation with the carry just now, if there is a carry, first write the small 1 of the carry below the adjacent high-side column, and take the ones digit to add to it. There is only a case of 1+1=10. No matter what carry is added to 0, there will be no carry. If there is no carry in the first calculation, there will be a carry only when a 1 is generated. In short, for each column, There will only be a carry at most once , so there is no need to worry about the problem that the carry will cross columns. The thing to pay attention to is not to write wrong numbers. For example, I have typed drafts twice before writing this paper, but I still make mistakes.
      For the case where there is a 1 in the high digit, move 1 to the lowest digit and do a binary one's complement calculation again, which is essentially the complement.
      (2) Calculate the binary complement of the results of the first and second lines and the third line, and so on.
      (3) The result of the operation is reversed and the checksum is obtained.

      In the hexadecimal version, the amount of calculation will be greatly reduced. The main calculation steps are as follows:
      (1) Starting from the first column on the right, calculate the value of the first column in decimal.
      The first column here is calculated as 107, written in 8-bit binary is 01101011.
      (2) Divide into two parts according to the calculated result. The left 4 digits are converted into decimal, which is used as the number added in the next column, and the right 4 digits are converted into hexadecimal as the result of the first column.
      Here is 610B16, the picture is miscalculated as D, 6 is the object to be added in the next column operation, and B is the result of the first column.
      (3) Calculate the result of the second column in decimal, add the carry obtained in the first column to get a decimal number in the second column, convert it to binary, judge according to the second step, and so on.
      The second column here is 24, and 6 is 30, which is 00011110 in 8-bit binary format, which is 1E. The result in the second column is 1, and the decimal carry in the third column is 1. Then it can be calculated that the result of the third column is 6, and the decimal carry is 4.
      (4) After calculating the final decimal result from the highest digit, when converting it to binary, the right 4 digits are used as the final result, and the left 4 is shifted into the next column. Use the result obtained in the previous step 96EB and add 0010 to get the final result. The last step is wrong and should not be discarded.
      The final result is 96ED.
      (5) Invert the result to get the check code.
      The check code is 6912.

      Okay, this time I first proposed a solution to this specific problem. If you have time, you need to sort out the knowledge about the computer's original code, inverse code, and complement.

Guess you like

Origin blog.csdn.net/qq_36171263/article/details/100335730