• 再论整型


    #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
    ",data_si);
    	printf("data_un = %u
    ",data_un);
    
    	var_long = (long long)data_si *762962736 / 100000000;
    	printf("var_long = %lld
    ",var_long);
    
    	un_var_long = (unsigned long long)(0x10000 - data_un)*762962736 / 100000000;
    	printf("un_var_long = %lld
    ",un_var_long);
    
    	int sian_ddd = -124;
    	printf("sian_ddd = %x
    ",sian_ddd);
    
    	var_long = -12399999999456;
    	printf("var_long = %lx
    ",var_long);
    	//截断后,依然保留了符号
    	sian_ddd = (int)var_long;
    	printf("sian_ddd = %x
    ",sian_ddd);
    
    	printf("long long = %x
    ",sizeof(long long));
    
    	sian_ddd = 0x80000000;
    	printf("sian_ddd--  = %d
    ",sian_ddd);
    	sian_ddd = 0x7fffffff;
    	printf("sian_ddd++  = %d
    ",sian_ddd);
    
    	sian_ddd = 0xE667A220;
    	printf("sian_ddd e6 = %d
    ",sian_ddd);
        printf("Hello world!
    ");
        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,但是编译器怎么来解析一个数据,就根据定义的类型:类型大小,整型,浮点,有符号、无符号。而且是根据补码来解析的

    
    

     

  • 相关阅读:
    pytest
    pytest 跳过测试用例 skip
    pytest
    pytest -allure标记用例级别severity
    pytest-
    pytest-分布式执行(pytest-xdist)
    4、win10搭建easy-mock平台
    pandas日期缺失,空值填充方法
    Pandas拼接操作(concat,merge,join和append)的区别
    关于docker容器和镜像的区别
  • 原文地址:https://www.cnblogs.com/CodeWorkerLiMing/p/13932320.html
Copyright © 2020-2023  润新知