c语言中按位取反 程序,C语言问题,if条件里面按位取反

#include

int main() {

unsigned char te1,te2,te3;

te1 = 0xff;

te2 = 0x00; //te2二进制:00000000

if(te3 == (~te1)){

te3 = 1;

}

else{

te3 = 2;

}

printf("%d",te3);//输出:2

printf("%u",(char)(~te1));//无符号char 输出是:0

//无符号char 输出解析:

//te1:0000 0000 0000 0000 0000 0000 1111 1111

//~te1:1111 1111 1111 1111 1111 1111 0000 0000

//~te1强制转换char:0000 0000 所以输出为0

printf("%u",(~te1)); //无符号 输出是:4294967040

//无符号 输出解析:

//te1:0000 0000 0000 0000 0000 0000 1111 1111

//~te1:1111 1111 1111 1111 1111 1111 0000 0000

//~te1=二进制1111 1111 1111 1111 1111 1111 0000 0000=4294967040

printf("%d",(~te1)); //有符号int 输出是:-256

//有符号 输出解析:

//te1:0000 0000 0000 0000 0000 0000 1111 1111

//~te1:1111 1111 1111 1111 1111 1111 0000 0000

//由于:~te1开头为1 是负数 补码输出

//~te1=1000 0000 0000 0000 0000 0000 1111 1111 + 1 = -256

if(-256 == (~te1)){

te3 = 1;

}

else{

te3 = 2;

}

printf("%d",te3);//输出:1

//说明在条件语句中(~te1)是按有符号输出 然后在做比较。

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值