=(~0);这是什么意思?
Possible Duplicates:
Is it safe to use -1 to set all bits to true?
int max = ~0; What does it mean?
Hello,
I have stumbled upon this piece of code..
size_t temp;
temp = (~0);
Anyone knows what it does?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
~
是按位非运算符,它将操作数的每一位取反。在这种情况下,操作数为 0,因此每个位最初都是 0,并且在按位应用后并不是每个位都为 1。最终结果是您得到一个充满 1 位的 size_t。~
is the bitwise not operator, it inverts each bit of the operand. In this case the operand is 0, so every bit is initially 0, and after applying the bitwise not every bit will be 1. The end result is you get a size_t filled with 1 bits.Sharptooth的答案是正确的,但为了给您更多细节,
~
是 不。基本上,您将NOT 0
的二进制等价值分配给 temp,这会将每一位设置为 1。sharptooth's answer is correct, but to give you more detail, the
~
is a binary operator for NOT. Basically, you're assigning the binary equivalent ofNOT 0
to temp and that will set every bit to 1.这是通常用于分配由所有二进制值构建的
size_t
值的一种方法,与size_t
类型的实际大小无关。如果这就是该代码的目的,则应该使用(size_t)( -1 )
。顺便说一句这是一个相同的问题。
That's one way typically used to assign a
size_t
value built of all binary ones independent of actual size ofsize_t
type. If that's the purpose of that code one should instead use(size_t)( -1 )
.Btw here's an identical question.
这个怎么样?
C++ 代码:
C 代码:
请查看 问题。
我认为这是更正确的方式。
How about this?
C++ code:
C code:
Please take a look the question.
I think it is more proper way.