这是您娱乐的例子
unsigned int myAdd(unsigned int a, unsigned int b)
{
unsigned int carry = a & b;
unsigned int result = a ^ b;
while(carry != 0)
{
unsigned int shiftedcarry = carry << 1;
carry = result & shiftedcarry;
result ^= shiftedcarry;
}
return result;
}
该循环可能会展开。它执行的次数取决于操作数中设置的位数,但永远不会大于的宽度unsigned int
。一旦carry
成为0
,接下来的迭代就不会改变任何东西。