C++ int 到 unsigned int 的转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4975340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
int to unsigned int conversion
提问by John
I'm just amazed to know that I can't convert signed to unsigned int by casting!
我很惊讶地知道我无法通过强制转换将有符号整数转换为无符号整数!
int i = -62;
unsigned int j = (unsigned int)i;
I thought I already knew this since I started to use casts, but I can't do it!
自从我开始使用演员表以来,我以为我已经知道这一点,但我做不到!
回答by James McNellis
You can convert an int
to an unsigned int
. The conversion is valid and well-defined.
您可以将 an 转换int
为unsigned int
. 转换有效且定义明确。
Since the value is negative, UINT_MAX + 1
is added to it so that the value is a valid unsigned quantity. (Technically, 2Nis added to it, where N is the number of bits used to represent the unsigned type.)
由于该值为负数,UINT_MAX + 1
因此将其添加到其中,使该值为有效的无符号数量。(从技术上讲,将 2 N添加到其中,其中 N 是用于表示无符号类型的位数。)
In this case, since int
on your platform has a width of 32 bits, 62 is subtracted from 232, yielding 4,294,967,234.
在这种情况下,因为int
在您的平台上有 32 位的宽度,所以从 2 32 中减去 62 ,得到 4,294,967,234。
回答by puzzle
Edit: As has been noted in the other answers, the standard actually guarantees that "the resulting value is the least unsigned integer congruent to the source integer (modulo 2n where n is the number of bits used to represent the unsigned type)". So even if your platform did not store signed ints as two's complement, the behavior would be the same.
编辑:正如其他答案中所指出的,标准实际上保证“结果值是与源整数一致的最小无符号整数(模 2n,其中 n 是用于表示无符号类型的位数)”。因此,即使您的平台没有将有符号整数存储为二进制补码,行为也会相同。
Apparently your signed integer -62 is stored in two's complement (Wikipedia) on your platform:
显然,您的有符号整数 -62 存储在您平台上的二进制补码(Wikipedia)中:
62 as a 32-bit integer written in binary is
62 作为以二进制编写的 32 位整数是
0000 0000 0000 0000 0000 0000 0011 1110
To compute the two's complement (for storing -62), first invert all the bits
要计算二进制补码(用于存储 -62),首先反转所有位
1111 1111 1111 1111 1111 1111 1100 0001
then add one
然后加一个
1111 1111 1111 1111 1111 1111 1100 0010
And if you interpret this as an unsigned 32-bit integer (as your computer will do if you cast it), you'll end up with 4294967234 :-)
如果你把它解释为一个无符号的 32 位整数(就像你的计算机在你转换它时所做的那样),你最终会得到 4294967234 :-)
回答by Stephen Canon
This conversion is well defined and will yield the value UINT_MAX - 61
. On a platform where unsigned int
is a 32-bit type (most common platforms, these days), this is precisely the value that others are reporting. Other values are possible, however.
此转换定义明确,将产生值UINT_MAX - 61
。在unsigned int
32 位类型的平台上(当今最常见的平台),这正是其他人报告的值。然而,其他值也是可能的。
The actual language in the standard is
标准中的实际语言是
If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2^n where n is the number of bits used to represent the unsigned type).
如果目标类型是无符号的,则结果值是与源整数一致的最小无符号整数(模 2^n,其中 n 是用于表示无符号类型的位数)。
回答by Himanshu Poddar
i=-62 . If you want to convert it to a unsigned representation. It would be 4294967234 for a 32 bit integer. A simple way would be to
我=-62。如果要将其转换为无符号表示。对于 32 位整数,它将是 4294967234。一个简单的方法是
num=-62
unsigned int n;
n = num
cout<<n;
4294967234
4294967234
回答by kubio
with a little help of math
在数学的帮助下
#include <math.h>
int main(){
int a = -1;
unsigned int b;
b = abs(a);
}
回答by Halllloooo
Since we know that i
is an int
, you can just go ahead and unsignedingit!
由于我们知道这i
是一个int
,您可以继续对其进行签名!
This would do the trick:
这可以解决问题:
int i = -62;
unsigned int j = unsigned(i);