将-1转换为无符号类型 [英] converting -1 to unsigned types

查看:191
本文介绍了将-1转换为无符号类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码来设置 x

unsigned int x = -1;

这是便携式的吗?它似乎至少可以在Visual Studio 2005-2010上运行

Is this portable ? It seems to work on at least Visual Studio 2005-2010

推荐答案

引文多的答案:

我知道这里有很多正确的答案,但是我想添加一些引用.我将列举两个标准:C99 n1256草案(免费提供)和C ++ n1905草案(免费提供).这些特定的标准没有什么特别之处,它们都是免费提供的,并且目前最容易找到的任何内容.

I know there are plenty of correct answers in here, but I'd like to add a few citations to the mix. I'll cite two standards: C99 n1256 draft (freely available) and C++ n1905 draft (also freely available). There's nothing special about these particular standards, they're just both freely available and whatever happened to be easiest to find at the moment.

C ++版本:

第5.3.2节¶9:根据本段,如果(type)为无符号类型.

§5.3.2 ¶9: According to this paragraph, the value ~(type)0 is guaranteed to have all bits set, if (type) is an unsigned type.

〜的操作数应为整数或枚举类型;结果是其操作数的补码.进行整体促销.结果的类型就是提升的操作数的类型.

The operand of ~ shall have integral or enumeration type; the result is the one’s complement of its operand. Integral promotions are performed. The type of the result is the type of the promoted operand.

第3.9.1节¶4:这说明了无符号数字溢出的工作原理.

§3.9.1 ¶4: This explains how overflow works with unsigned numbers.

声明为无符号的无符号整数应遵循2 n 的算术定律,其中n是该特定整数大小的值表示形式中的位数.

Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo 2n where n is the number of bits in the value representation of that particular size of integer.

第3.9.1节¶7,加上脚注49:这说明数字必须为二进制.据此,我们可以推断出〜(type)0 必须是可以用 type 表示的最大数字(因为它将所有位都打开,并且每个位都是加法的).

§3.9.1 ¶7, plus footnote 49: This explains that numbers must be binary. From this, we can infer that ~(type)0 must be the largest number representable in type (since it has all bits turned on, and each bit is additive).

整数类型的表示应通过使用纯数来定义值二进制计数系统 49 .

49)使用二进制数字0和1的整数位置表示法开始,其中连续位表示的值是加法运算与1的乘积,再乘以2的连续积分乘数,除了可能具有最高位置的钻头.(改编自 American National信息处理系统词典.)

49) A positional representation for integers that uses the binary digits 0 and 1, in which the values represented by successive bits are additive, begin with 1, and are multiplied by successive integral power of 2, except perhaps for the bit with the highest position. (Adapted from the American National Dictionary for Information Processing Systems.)

由于算术是对2 n 取模,因此可以保证(type)-1 是该类型可表示的最大值.还可以保证〜(type)0 是该类型可表示的最大值.因此,它们必须相等.

Since arithmetic is done modulo 2n, it is guaranteed that (type)-1 is the largest value representable in that type. It is also guaranteed that ~(type)0 is the largest value representable in that type. They must therefore be equal.

C99版本:

C99版本以更紧凑,更明确的方式说明了这一点.

The C99 version spells it out in a much more compact, explicit way.

第6.5.3节¶3:

〜运算符的结果是其(提升的)操作数的按位补码(即,当且仅当转换后的操作数中的对应位为没有设置).对操作数执行整数提升,结果为提升型.如果提升的类型是无符号类型,则表达式〜E等价达到该类型减去E所能表示的最大值.

The result of the ~ operator is the bitwise complement of its (promoted) operand (that is, each bit in the result is set if and only if the corresponding bit in the converted operand is not set). The integer promotions are performed on the operand, and the result has the promoted type. If the promoted type is an unsigned type, the expression ~E is equivalent to the maximum value representable in that type minus E.

就像在C ++中一样,无符号算术可以保证是模块化的(我想我现在已经对标准进行了足够的挖掘),因此C99标准肯定可以保证〜(type)0 ==(type)-1 ,从§6.5.3¶3知道〜(type)0 必须设置所有位.

As in C++, unsigned arithmetic is guaranteed to be modular (I think I've done enough digging through standards for now), so the C99 standard definitely guarantees that ~(type)0 == (type)-1, and we know from §6.5.3 ¶3 that ~(type)0 must have all bits set.

摘要:

是的,它是便携式的.无符号类型x = -1; 保证所有位都根据标准设置.

Yes, it is portable. unsigned type x = -1; is guaranteed to have all bits set according to the standard.

脚注:是的,我们谈论的是值位,而不是填充位.我怀疑您是否需要将填充位设置为1.您可以从最近的堆栈溢出问题中看到(链接),则GCC已移植到PDP-10,其中 long long 类型只有一个填充位.在这样的系统上, unsigned long long x = -1; 可能不会将该填充位设置为1.但是,只有使用指针强制转换时,您才能发现这一点.还是可以随身携带的.

Footnote: Yes, we are talking about value bits and not padding bits. I doubt that you need to set padding bits to one, however. You can see from a recent Stack Overflow question (link) that GCC was ported to the PDP-10 where the long long type has a single padding bit. On such a system, unsigned long long x = -1; may not set that padding bit to 1. However, you would only be able to discover this if you used pointer casts, which isn't usually portable anyway.

这篇关于将-1转换为无符号类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆