C不能将运算符应用于int吗? [英] C not operator applied to int?

查看:114
本文介绍了C不能将运算符应用于int吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

int x = 5;
printf("%d", x); //i get 5... expected

x = !x;
printf("%d", x);// i get 0... hmm 

5的二进制是:0101 如果对每个位应用逆,则应该得到1010,但是!不一定是反相器,它是逻辑运算符.为什么我会得到0?

5 in binary is: 0101 if we apply the inverse to each bit, we should get 1010, but ! is not necessarily an inverter, it's a logical operator. Why do i get a 0 ?

是在C中将正数视为true的原因,因此!-导致结果为0? 这个编译器是特定的吗?

is the reason that, in C, a positive number is treated as true and so !-ing it would result in 0? is this compiler specific?

推荐答案

not(!)运算符分别根据输入是非零还是0来返回01.

The not (!) operator returns either 0 or 1, depending on whether the input is non-zero or 0 respectively.

如果您希望按位取反,请尝试~x.

If you are looking for a bitwise negation, try ~x.

这篇关于C不能将运算符应用于int吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆