我有:
int a = 2147483647;
short b = (short)a;
我得到
b = -1
,而我希望int32
可以转换为int16
(short
)。我希望看到一些值和而不是 -1
。请有人帮助我。
最佳答案
您的int A大于short的大小。将A转换为short时,最左边的位为1,这将表明它为负数。因为您得到-1,所以我想您在所有16位中都得到1,这将为您提供-2 ^ 15 + 2 ^ 14 + 2 ^ 13 ... + 2 ^ 0,这将为您提供-1。简而言之(不需要双关语),如果整数太大,则不能将其转换为短整数。
关于c - 在C中将int转换为short,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63589995/