Possible duplicates:Is it possible to use -1 to set all bits to true?int max = ~ 0; What does it mean?
Hello,
I came across this piece of code.
size_t temp; temp = (~0);
Does anyone know what he is doing?
~ is a bitwise operator; it inverts every bit of the operand. In this case, the operand is 0, so each bit is initially 0, and after applying bitwise, not every bit will be 1. The end result is getting size_t with 1 bit.
~
This method is usually used to assign a value to size_t , constructed from all binary, regardless of the actual size of type size_t . If for this purpose you should use (size_t)( -1 ) instead.
size_t
(size_t)( -1 )
Btw here is an identical question .
How about this?
C ++ Code:
#include <limits> std::size_t temp = std::numeric_limits<std::size_t>::max();
C code: Please see the question .
I think this is the more correct way.
sharptooth answer is correct, but for more details ~ is a binary operator for NOT . Basically, you assign the binary equivalent of NOT 0 to temp, and each bit should set 1.
NOT 0