首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则ES.101:使用无符号类型进行位操作

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

作者头像
面向对象思考
发布2020-06-17 19:07:05
3060
发布2020-06-17 19:07:05
举报

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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-06-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Example(示例)
  • Note(注意)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档