且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

这是在C中实现+运算符的方式吗?

更新时间:2023-11-10 09:32:34

要学究,C规范未指定如何加法实现.

To be pedantic, the C specification does not specify how addition is implemented.

但实际上,小于或等于CPU字长的整数类型上的+运算符被直接转换为CPU的加法指令,较大的整数类型被转换为具有某些内容的多个加法指令多余的位来处理溢出.

But to be realistic, the + operator on integer types smaller than or equal to the word size of your CPU get translated directly into an addition instruction for the CPU, and larger integer types get translated into multiple addition instructions with some extra bits to handle overflow.

CPU在内部使用逻辑电路来实现加法运算,并且不使用循环,移位或与C的工作方式非常相似的任何东西.

The CPU internally uses logic circuits to implement the addition, and does not use loops, bitshifts, or anything that has a close resemblance to how C works.