C not operator applied to int?(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 中,正数被视为真,所以 !-ing 它会导致 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 或 1,分别取决于输入是非零还是 0.
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C不是运算符应用于int?
基础教程推荐
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- CString 到 char* 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
