CSAPP(一)

0x01 关于补码

iHATE公式

对于char类型

2Byte,8位,其中有1位视为符号位
表示最大值

2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
0 1 1 1 1 1 1 1

UMax: -2^7*0 + 2^6 + 2^5 + 2^4 + 2^3 + 2^2 + 2^1 + 1 = 127
转为16进制表示即:7F

TMax: -2^7 + 2^6 + 2^5 + 2^4 + 2^3 + 2^2 + 2^1 + 1 = -1
转为16进制表示即:FF

0x02 互相转换

//当signed number与unsigned number比较时,会将signed number进行隐式转换
#include <stdio.h>

int main()
{
    //1Byte=8Bit
    //c语言是静态语言即声明时要指定类型,强类型语言
    char x1=0x7f;
    char x2=0xff;
    unsigned char x3=0xff;
    printf("%d\n",x1);
    printf("%d\n",x2);
    if (x2<x1)
    {
        printf("this is signed number compare\n");
    }

    printf("x2=%d\n",x2);
    if (x3>x1)
    {
        printf("this is unsigned char compare\n");
        printf("x3=%d",x3);
    }

    return 0;
}

只要有任意1个参数是unsigned number那么其他的参数在进行运算时均会转为unsigned number

posted @ 2024-07-30 15:23  Cia1lo  阅读(9)  评论(0编辑  收藏  举报