Why must a short be converted to an int before arithmetic operations in C and C++?

后端 未结 4 1685
情深已故
情深已故 2020-11-22 05:25

From the answers I got from this question, it appears that C++ inherited this requirement for conversion of short into int when performing arithmet

4条回答
  •  無奈伤痛
    2020-11-22 05:56

    It's not a feature of the language as much as it is a limitation of physical processor architectures on which the code runs. The int typer in C is usually the size of your standard CPU register. More silicon takes up more space and more power, so in many cases arithmetic can only be done on the "natural size" data types. This is not universally true, but most architectures still have this limitation. In other words, when adding two 8-bit numbers, what actually goes on in the processor is some type of 32-bit arithmetic followed by either a simple bit mask or another appropriate type conversion.

提交回复
热议问题