zoukankan      html  css  js  c++  java
  • 再论整型

    #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,但是编译器怎么来解析一个数据,就根据定义的类型:类型大小,整型,浮点,有符号、无符号。而且是根据补码来解析的

    
    

     

  • 相关阅读:
    Linux 下卸载MySQL 5
    Solr使用入门指南
    GridView行编辑、更新、取消、删除事件使用方法
    从最大似然到EM算法浅解
    MySQL中数据表的增操作
    趣味Java算法题(附答案)
    nodeValue的兼容问题
    边记边学PHP-(十五)MySQL数据库基础操作2
    素数推断算法(高效率)
    expect
  • 原文地址:https://www.cnblogs.com/CodeWorkerLiMing/p/13932320.html
Copyright © 2011-2022 走看看