且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

算术溢出等于模运算吗?

更新时间:2023-11-10 16:20:52

否.没有什么可以保证unsigned char具有八位.使用<stdint.h>中的uint8_t,您会很好的.这需要支持stdint.h的实现:任何符合C99的编译器都支持,但是较早的编译器可能不提供.

No. There is nothing that guarantees that unsigned char has eight bits. Use uint8_t from <stdint.h>, and you'll be perfectly fine. This requires an implementation which supports stdint.h: any C99 compliant compiler does, but older compilers may not provide it.

注意:无符号算术永远不会溢出,并且表现为模2 ^ n".带符号的算术溢出并具有未定义的行为.

Note: unsigned arithmetic never overflows, and behaves as "modulo 2^n". Signed arithmetic overflows with undefined behavior.