开发者

= (~0); What does it mean? [duplicate]

This question already开发者_JS百科 has answers here: Closed 11 years ago.

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?


~ 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's answer is correct, but to give you more detail, the ~ is a binary operator for NOT. Basically, you're assigning the binary equivalent of NOT 0 to temp and that will set every bit to 1.


That's one way typically used to assign a size_t value built of all binary ones independent of actual size of size_t type. If that's the purpose of that code one should instead use (size_t)( -1 ).

Btw here's an identical question.


How about this?

C++ code:

#include <limits>

std::size_t temp = std::numeric_limits<std::size_t>::max();

C code: Please take a look the question.

I think it is more proper way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜