c语言余一个负数_c语言是如何取一个数的负数的?

本文探讨了C语言中负数的计算方式,特别是针对无符号整数的取负操作。引用了C99、K&R C和C++11的规范,并通过实例解释了无符号数和有符号数取负的过程,涉及到了整型提升、补码计算等概念。结论是C语言中取负操作实际上是通过求补码实现的。
摘要由CSDN通过智能技术生成

以下全都是参考这里回答的:http://stackoverflow.com/questions/8026694/c-unary-minus-operator-behavior-with-unsigned-operands

(1)C99中是这样解释“-”操作符:The result of the unary - operator is the negative of its (promoted) operand. The integer

promotions are performed on the operand, and the result has the promoted type.

但是关于怎么计算什么也没有说。。。

(2)The C programming Language(K&R C)是这样解释“-”操作符:A.7.4.5 Unary Minus Operator

The operand of the unary - operator must have arithmetic type, and the result is the negative of its operand. An integral operand undergoes integral promotion. The negative of an unsigned quantity is computed by subtracting the promoted value from the largest value of the promoted type and adding one;but negative zero is zero. The type of the result is the type of the promoted operand.

这里说了对整型数取负需要进行整型提升,特别说明了对于unsinged int取负的计算方法。

(3)C++11中是这样解释“-”操作符:The

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值