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

本文探讨了C语言中对负数使用"-"操作符的行为,包括无符号整数和有符号整数的情况。通过引用C标准、K&R C和C++11的解释,解释了无符号数取负是通过从最大值减去值加一来计算的。通过实例展示了取负和求补码的关系,并提到了《深入理解计算机系统》中关于取补码的方法。总结指出,取负操作实际是对操作数进行求补码的过程。
摘要由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、付费专栏及课程。

余额充值