关于C中的整数倒数,例如
一个int值000F'E000向下转换为short或unsigned short将成为E000.
short- > -8192,
unsigned short - > 57344,
那么它只是削减了比特吗?
那些上流社会呢?例如,int -10是ffffff81,要施加什么规则long long?
@Update
关于upcasting,根据答案,我做了一些测试,发现有2的补码它有以下规则:
signed:
positive -> positive: add 0 as prefix bits,
negative -> negative: add 1 as prefix bits,
unsigned:
add 0 as prefix bits,
码:
// integer numbers, downcast & upcast,
#include <stdio.h>
void downcastTest() {
int i = 127<<13;
printf("%X, %hX, %hi\n", i, i, i);
}
void upcastTest() {
int i = 127;
int j = -127;
printf("%x, %llx\n", i, (long long)i);
printf("%x, %llx\n", j, (long long)j);
}
int main(int argc, char * argv[]) {
downcastTest();
upcastTest();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
abl*_*igh 13
转换为较小的整数类型会丢弃目标类型中不存在的最重要的(在纸上写入完整二进制整数时最左边)位.
向上转换为更大的整数更复杂:
| 归档时间: |
|
| 查看次数: |
5064 次 |
| 最近记录: |