gpt4 book ai didi

c++ - 是否保证按字节的零 int 是零的表示?

转载 作者:行者123 更新时间:2023-12-02 02:52:28 26 4
gpt4 key购买 nike

特别是,以下断言保证成立:

int i;
memset(&i, 0, sizeof(int));
assert(i == 0);

最佳答案

C++ 中的

int 可以有 ones' complement , two's complement ,或signed magnitude二进制表示法 [basic.fundamental]/7 :

Types bool, char, char16_­t, char32_­t, wchar_­t, and the signed and unsigned integer types are collectively called integral types. A synonym for integral type is integer type. The representations of integral types shall define values by use of a pure binary numeration system. [ Example: This International Standard permits two's complement, ones' complement and signed magnitude representations for integral types.  — end example ]

在每种表示形式中,对于该整数类型来说,全零位的值为零,因此可以保证断言始终为真。

关于c++ - 是否保证按字节的零 int 是零的表示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59883915/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com