类型转换为无符号用C [英] typecasting to unsigned in C
问题描述
int a = -534;
unsigned int b = (unsigned int)a;
printf("%d, %d", a, b);
打印 -534,-534
为什么不强制转换发生?
Why is the typecast not taking place?
我希望它是 -534,534
如果我修改code到
int a = -534;
unsigned int b = (unsigned int)a;
if(a < b)
printf("%d, %d", a, b);
它不是打印毕竟什么... A
小于 B
??
推荐答案
首先,你不需要投:的 A
隐式转换为<值code> unsigned int类型与分配给 b
。所以,你的语句等价于:
First, you don't need the cast: the value of a
is implicitly converted to unsigned int
with the assignment to b
. So your statement is equivalent to:
unsigned int b = a;
现在,一个重要的特性无符号
整型的C和C ++的是,它们的值总是在范围[0,的最大的] ,其中的最大的为 unsigned int类型
是 UINT_MAX
(它定义在 limits.h中
)。如果您分配一个值,这不是在这个范围内,它被转换到该范围内。因此,如果该值是负的,您可以添加 UINT_MAX + 1
反复使其在区间[0, UINT_MAX
]。对于上面的code,这是因为如果我们写: unsigned int类型B =(UINT_MAX + A)+ 1
。这不等于 -a
(534)。
Now, an important property of unsigned
integral types in C and C++ is that their values are always in the range [0, max], where max for unsigned int
is UINT_MAX
(it's defined in limits.h
). If you assign a value that's not in that range, it is converted to that range. So, if the value is negative, you add UINT_MAX+1
repeatedly to make it in the range [0, UINT_MAX
]. For your code above, it is as if we wrote: unsigned int b = (UINT_MAX + a) + 1
. This is not equal to -a
(534).
请注意,上面的是真实的底层重新presentation是否补,一补数,或登录幅度(或任何其它特殊编码)。人们可以看到的东西,如:
Note that the above is true whether the underlying representation is in two's complement, ones' complement, or sign-magnitude (or any other exotic encoding). One can see that with something like:
signed char c = -1;
unsigned int u = c;
printf("%u\n", u);
assert(u == UINT_MAX);
在一个典型的补机具有4个字节的 INT
, C
是 0xFF的
和 U
是为0xffffffff
。编译器必须确保当值 1
分配给 U
,它被转换成等于值 UINT_MAX
。
On a typical two's complement machine with a 4-byte int
, c
is 0xff
, and u
is 0xffffffff
. The compiler has to make sure that when value -1
is assigned to u
, it is converted to a value equal to UINT_MAX
.
现在回到你的code时,的printf
格式字符串是错误的 B
。您应该使用%U
。当你这样做,你会发现它打印的值UINT_MAX - 534 + 1
而不是 534
Now going back to your code, the printf
format string is wrong for b
. You should use %u
. When you do, you will find that it prints the value of UINT_MAX - 534 + 1
instead of 534
.
当在比较操作使用&LT;
,因为 B
是无符号整型
, A
也转换为 unsigned int类型
。这与给出B = A
;早些时候,意味着 A&LT; b
是假的: A
为 unsigned int类型
等于乙
。
When used in the comparison operator <
, since b
is unsigned int
, a
is also converted to unsigned int
. This, given with b = a
; earlier, means that a < b
is false: a
as an unsigned int
is equal to b
.
让我们假设你有一个一补数机,以及你做的:
Let's say you have a ones' complement machine, and you do:
signed char c = -1;
unsigned char uc = c;
假设一个字符
(符号或无符号)是机器上的8位。然后 C
和 UC
将存储下列值,位模式:
Let's say a char
(signed or unsigned) is 8-bits on that machine. Then c
and uc
will store the following values and bit-patterns:
+----+------+-----------+
| c | -1 | 11111110 |
+----+------+-----------+
| uc | 255 | 11111111 |
+----+------+-----------+
注意的ç
和 UC
的位模式是不一样的。编译器必须确保 C
值为 1
和 UC
值为 UCHAR_MAX
,这是255在此计算机上。
Note that the bit patterns of c
and uc
are not the same. The compiler must make sure that c
has the value -1
, and uc
has the value UCHAR_MAX
, which is 255 on this machine.
有上<更详细href=\"http://stackoverflow.com/questions/1863153/why-unsigned-int-0xffffffff-is-equal-to-int-1/1863219#1863219\">my回答一个问题在这里SO 。
这篇关于类型转换为无符号用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!