将int转换为C中的short [英] convert int to short in C
问题描述
我有:
int a = 2147483647;
short b = (short)a;
我得到 b = -1
我希望 int32
转换为 int16
( short
) 。我希望看到一些价值和不 -1
。
and I get b = -1
whereas I expect int32
to be converted to int16
(short
). I expect to see some value and not -1
.
请有人帮帮我。
推荐答案
你的int A大于短的大小。将A转换为short时,最左边的位数为1,这表示它是负数。因为你得到-1,我想你在所有16位中得到1,这将给你-2 ^ 15 + 2 ^ 14 + 2 ^ 13 ... + 2 ^ 0,这将给你-1。简而言之(没有双关语),如果它太大,则无法将整数转换为short。
Your int A is larger than the size of short. When you convert A to short, you get a 1 in the left most bit, which is going to indicate that it is a negative number. Since you're getting -1, I suppose you're getting 1s in all 16 bits, which is going to give you -2^15 + 2^14 + 2^13... + 2^0, which will give you -1. In short (no pun intended), you can't convert the integer to a short if it is too large.
这篇关于将int转换为C中的short的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!