无符号数相减_C++核心准则ES.101:使用无符号类型进行位操作

ea6e13ce9a6a77dcb256d80b474acb2d.png

ES.101: Use unsigned types for bit manipulation

ES.101:使用无符号类型进行位操作

Reason(原因)

Unsigned types support bit manipulation without surprises from sign bits.

无符号类型支持不受符号位干扰的位操作。

Example(示例)

unsigned char x = 0b1010'1010;unsigned char y = ~x;   // y == 0b0101'0101;

Note(注意)

Unsigned types can also be useful for modulo arithmetic. However, if you want modulo arithmetic add comments as necessary noting the reliance on wraparound behavior, as such code can be surprising for many programmers.

无符号类型在模运算时也很有用。然而,如果你想使用模运算的话,增加必要的注释说明结果会依赖环绕行为,因为这样的代码会让很多程序员感到迷惑。

Enforcement(实施建议)

  • Just about impossible in general because of the use of unsigned subscripts in the standard library
  • 基本上不可能,由于标准库使用无符号数作为下标。
  • ???

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es101-use-unsigned-types-for-bit-manipulation


觉得本文有帮助?请分享给更多人。

关注微信公众号【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值