4

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?

Community
  • 1
  • 1
Stefanos Kalantzis
  • 1,619
  • 15
  • 23
  • 3
    http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Bitwise_operators – Bart May 11 '11 at 07:18
  • 1
    @Bart: What a *marvelous* website! – user541686 May 11 '11 at 07:19
  • 3
    This is an **erroneous** approach to getting the maximum value. You should instead assign it the value `-1`, which is guaranteed to behave properly. See the discussion [here](http://stackoverflow.com/questions/809227/is-it-safe-to-use-1-to-set-all-bits-to-true). (Effectively a duplicate, considering the amount of information in the linked question.) – GManNickG May 11 '11 at 07:23
  • @GManNickG But size_t is unsigned. – Vincent Apr 17 '16 at 18:32
  • @Vincent: Did you read the link I provided? – GManNickG Apr 17 '16 at 22:40

4 Answers4

2

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.

Community
  • 1
  • 1
sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • I don't think there's `static_cast` in **C**. He did say C. – cnicutar May 11 '11 at 07:19
  • 3
    I won't -1 because it's a common misconception, but that's incorrect. They are, in fact, different. Assigning `-1` to an unsigned type is guaranteed to result in the largest value that type can hold. Contrarily, assigning `~0` could, on a one's compliment machine, end up with the value zero. There are other pitfalls, too; see the discussion [here](http://stackoverflow.com/questions/809227/is-it-safe-to-use-1-to-set-all-bits-to-true). – GManNickG May 11 '11 at 07:21
  • ok cool thanks ! SO did not find the similar one.. neither did I. Sorry ! – Stefanos Kalantzis May 11 '11 at 07:22
  • @GMan: out of curiosity: are there *any* one's compliment machines in active use out there? – Joachim Sauer May 11 '11 at 07:23
  • @GMan: Thank you, I couldn't find that question. I'll edit the answer. – sharptooth May 11 '11 at 07:24
  • @Joachim: Probably in some esoteric facility somewhere, who knows. – GManNickG May 11 '11 at 07:24
  • @Joachim: in the many times this has come up on SO, I think someone has named a 1s' complement machine that had a C compiler, some of which are still capable of operating (if not necessarily in active use and not in production). Can't remember what. – Steve Jessop May 11 '11 at 08:40
2

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.

Antony Woods
  • 4,415
  • 3
  • 26
  • 47
2

~ 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.

verdesmarald
  • 11,646
  • 2
  • 44
  • 60
  • 1
    The result is that of converting an `int` filled with 1 bits to `size_t`. With two's complement, that's a `size_t` filled with 1 bits. – Steve Jessop May 11 '11 at 08:37
  • @SteveJessop size_t is an unsigned type – Vincent Apr 17 '16 at 18:32
  • @Vincent: But `int` is signed, which is why the representation affects the `int` -> `size_t` conversion. If `int` were ones' complement then the result would be `0`, and if `int` were sign-magnitude then the result would be `(size_t)(INT_MAX) + 2`. – Steve Jessop Apr 18 '16 at 09:00
2

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.

Community
  • 1
  • 1