C语言操作数截断

//测试截断
#include <stdio.h>
int main()
{
    
    int a = 0x80000001;
    unsigned int b = 0x80000001;
    printf("有符号数a的值是%d
", a);
    printf("无符号数b的值是%x
", b);

    //有符号数与无符号数的截断
    short c = (short)a;
    short c1 = (short)b;
    printf("有符号数a截断后的值是%hd
", c);
    printf("无符号数b截断后的值是%hd
", c1);

    //补码截断
    a = -1;
    printf("有符号数a的补码截断前的值是%x
", a);
    short d = (short)a;
    short d1 = (short)b;
    printf("有符号数a的补码截断后的值是%hx
", d);
    printf("有符号数a的补码截断后的值是%d
", d);
    printf("无符号数b的补码截断后值是%hd
", d1);

    return 0;
}

原文地址:https://www.cnblogs.com/zzdbullet/p/9494192.html