6

I've seen both (size_t)-1 and ~0 used to represent large numbers, or numbers with all their bits flipped.

Is there any difference between the two? If so, what is it?

I found this question: What is the difference between -1 and ~0, however it did not answer my question because I'm dealing with unsigned integers (such as size_t), as opposed to signed integers (such as int).

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
  • 1
    Note that `~0` is a signed quantity — you'd need `~0U` to make it unsigned. – Jonathan Leffler Apr 14 '19 at 18:56
  • Should I edit that? – S.S. Anne Apr 14 '19 at 18:58
  • 3
    Since you've got some answers which addresses `~0` rather than `~0U`, best to leave it unchanged, I think, but note for the future that it is a good idea to be careful. You can change a question up until making the change would invalidate answers. – Jonathan Leffler Apr 14 '19 at 19:05
  • related: [Is static_cast(-1) the right way to generate all-one-bits data without numeric_limits?](https://stackoverflow.com/q/36675858/995714), [What is the purpose of "int mask = ~0;"?](https://stackoverflow.com/q/46376693/995714) – phuclv Apr 15 '19 at 01:47
  • Differences: First is C++. Second is not unsigned. – S.S. Anne Apr 15 '19 at 11:03

3 Answers3

6

What's the difference between (size_t)-1 and ~0?

Type and value differ.

(size_t)-1 is the same value as SIZE_MAX and has a type of size_t.

~0 is often -1 and has the type of int.


Assigning both of those to a size_t will result in SIZE_MAX.

size_t a = (size_t)-1; 
size_t b = ~0;

In the 2nd case, -1 is assigned to a b and undergoes a conversion first, wrapping around the -1 to the maximum size_t value.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/191818/discussion-on-answer-by-chux-whats-the-difference-between-size-t-1-and-0). – Bhargav Rao Apr 14 '19 at 19:51
3

(size_t)-1 is of type size_t. It typically has a value of 232-1 or 264-1 (4294967295 or 18446744073709551615).

~0 is of type int, and has the value -1 on a 2's-complement system (i.e., just about everywhere).

Both are likely to have the same bit pattern -- if int and size_t are the same size, which they very commonly are not.

If you want the maximum value of type size_t, you can use the SIZE_MAX macro, defined in <stdint.h>. If you're using an older implementation (pre-C99) that doesn't provide SIZE_MAX, (size_t)-1 will work. I'm not sure why you'd want to write ~0 rather than -1 -- unless perhaps you're considering non-two's-complement systems.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • Thanks for letting me know. I'll remember to define `SIZE_MAX` in my `stdint.h` implementation. – S.S. Anne Apr 14 '19 at 19:02
  • Re; `~0` versus `-1` - some compilers warn on implicit wrapping in constant expressions. – TLW Apr 14 '19 at 21:43
2

Note that the previous answers assume a 2's complement machine (very likely to be the case these days, but not guaranteed).

If you had a sign-magnitude machine then -1 would have a sign bit and least significant bit set with all others clear, if you had a 1's complement machine then -1 would have all bits but the LSB set.

In all of these cases (including the common 2's complement machine), ~0 has all bits set.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
  • Note that in the question I'm asking about *unsigned* integers, so there's no such thing as a "sign bit". – S.S. Anne Apr 14 '19 at 20:56
  • 2
    @JL2210 - note that the intermediate values of both `~0` and `-1` are signed, not unsigned. – TLW Apr 14 '19 at 21:41
  • Ah. I understand now. I'll upvote as soon as this is edited. – S.S. Anne Apr 14 '19 at 22:02
  • Concerning non-2s-complement: The most recent non-2's complement machine I have heard of was from somewhere in 2000-2008 and I have not used one since pre-1990. Do you know of any in use in the last 10 years? – chux - Reinstate Monica Apr 15 '19 at 00:54