C++核心准则ES.101:使用无符号类型进行位操作
共 906字,需浏览 2分钟
·
2020-06-12 23:21
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
觉得本文有帮助?请分享给更多人。
关注微信公众号【面向对象思考】轻松学习每一天!
面向对象开发,面向对象思考!