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?
基础教程推荐
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- c++ STL设置差异 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 提升 ASIO 流缓冲 2021-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
