问题描述
INT最大=〜0;
int max = ~0;
是什么意思
推荐答案
的〜
运算符是一元按位补运算而计算位元补码。这意味着,它颠倒所有位在其参数(0变成1秒和1变成0)。因此,
The ~
operator is the unary bitwise complement operator which computes the bitwise complement. This means that it reverses all the bits in its argument (0s become 1s and 1s become 0s). Thus,
int max = ~0;
这是设置最大
来的否定32位值 0000 0000 0000 0000 0000 0000 0000 0000
产生 1111 1111 1111 1111 1111 1111 1111 1111
。由于我们存储这个结果在的Int32
,这是一样的 1
。
which is setting max
to the negation of the 32-bit value 0000 0000 0000 0000 0000 0000 0000 0000
resulting in 1111 1111 1111 1111 1111 1111 1111 1111
. As we are storing this result in an Int32
, this is the same as -1
.
不管它是不是不如说
int max = ~0;
或
int max = -1;
取决于上下文。如果最大
的一点是要有一批所有的位 1
我会选择前者。如果最大
的一点是要计算最大非负整数的清单,我会选择后者(当然,我宁愿 INT最大= Int32.MinValue;
更应如此,我只是喜欢 INT最大= list.Max();
)
depends on the context. If the point of max
is to have a number all of whose bits are 1
I would choose the former. If the point of max
is to compute the maximum of a list of non-negative integers, I would choose the latter (well, I'd prefer int max = Int32.MinValue;
and even more so, I'd just prefer int max = list.Max();
).
这篇关于INT最大=〜0;这是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!