函数执行什么操作? [英] What operation does the function perform?

查看:32
本文介绍了函数执行什么操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int function(uint32_t *r, const uint32_t *a, const uint32_t *b, int n)
{
 int i;
 uint32_t ri, c=0;
 for (i = 0; i < n; i ++)
 {
   ri = a[i] + b[i] + c;
   c = ((ri < a[i]) || ((ri == a[i]) && c));
   r[i] = ri;
 }
   return ((int) c);
}

下面给出的 C 函数有四个参数:r、a 和 b 是指向数组的指针uint32_t 类型.整数 n 指定这些数组的长度(即所有三个数组包含相同数量的元素).返回值是 int 类型.任何人都可以帮助我了解此功能执行的操作吗?

The C function given below has four arguments: r, a, and b are pointers to arrays of type uint32_t. The integer n specifies the length of these arrays (i.e. all three arrays contain the same number of elements). The return value is of type int. Can anyone could help me to understand the operation performed by this function?

推荐答案

它正在执行带有进位传播的多精度加法.ab 参数是指向多精度整数的指针,每个整数都有 n 位.在这种情况下,数字是 32 位.最低有效数字位于最低的数组索引中.

It is performing multi-precision addition with carry propagation. The a and b arguments are pointers to multi-precision integers with n digits each. In this case, the digits are 32-bits. The least-significant digits are in the lowest array indices.

输入相加,结果放入r指向的数组中(也包含n 32位数字).它的工作原理是将 a 中的一个数字添加到 b 中的一个数字,并带有进位 c,它被初始化为零.当结果位小于输入位之一时检测进位,或当进位为 1 时等于其中一位.返回值是整个操作的进位.

The inputs are added, and the result is placed into the array pointed to by r (also containing n 32-bit digits). It works by adding a digit from a to a digit from b with the carry-in c, which is initialized to zero. A carry out is detected when the resulting digit is less than one of the input digits, or is equal to one of the digits when the carry-in is 1. The return value is the carry-out from the whole operation.

想象一下,我们正在添加以 10 为底的数字.如果我们计算 9+9+0 mod 10,我们得到 8.由于 8 小于 9,我们推断一定存在进位.如果我们计算 9+9+1 模 10,我们得到 9;我们推断出结转是因为设置了结转.如果我们计算 9+0+0,我们得到 9,但没有进位,因为进位是 0.

Imagine we are adding with base-10 digits. If we compute 9+9+0 mod 10, we get 8. Since 8 is less than 9, we infer that there must have been a carry-out. If we compute 9+9+1 modulo 10, we get 9; we infer a carry-out because the carry-in was set. If we compute 9+0+0, we get 9, but there was no carry-out because carry-in was 0.

这篇关于函数执行什么操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆