再论整型

#include <stdio.h>
#include <stdlib.h>

int main()
{
	unsigned long long un_var_long = 0;
	long long var_long = 0;
	unsigned int data_un = 0x8000;
	int data_si = 0x8000|0xffff0000;
	//int data_si = 0x7FFF;
	printf("data_si = %d\n",data_si);
	printf("data_un = %u\n",data_un);

	var_long = (long long)data_si *762962736 / 100000000;
	printf("var_long = %lld\n",var_long);

	un_var_long = (unsigned long long)(0x10000 - data_un)*762962736 / 100000000;
	printf("un_var_long = %lld\n",un_var_long);

	int sian_ddd = -124;
	printf("sian_ddd = %x\n",sian_ddd);

	var_long = -12399999999456;
	printf("var_long = %lx\n",var_long);
	//截断后,依然保留了符号
	sian_ddd = (int)var_long;
	printf("sian_ddd = %x\n",sian_ddd);

	printf("long long = %x\n",sizeof(long long));

	sian_ddd = 0x80000000;
	printf("sian_ddd--  = %d\n",sian_ddd);
	sian_ddd = 0x7fffffff;
	printf("sian_ddd++  = %d\n",sian_ddd);

	sian_ddd = 0xE667A220;
	printf("sian_ddd e6 = %d\n",sian_ddd);
    printf("Hello world!\n");
    return 0;
}

内存截图:只运行到sian_ddd = 0x80000000;之前的情况下。后续情况自行推导。

0x61fe04: 20 a2 67 e6 = -429415904
0x61fe08: 00 80 ff ff = -32768
0x61fe0c: 00 80 00 00 = 0x8000
0x61fe10: 20 a2 67 e6 b8 f4 ff ff = -12399999999456
0x61fe18: 97 d0 03 00 00 00 00 00 = 250007

变量信息

小结:

long long 转换为int类型是直接截断的,因此不确保int的符号位,因此需要特别注意,测试环境是gcc,x86的架构,因此是小端模式。
内存的信息都是0101,但是编译器怎么来解析一个数据,就根据定义的类型:类型大小,整型,浮点,有符号、无符号。而且是根据补码来解析的


 

posted @ 2020-11-05 16:16  wdliming  阅读(90)  评论(0编辑  收藏  举报