6

Is it safe to assign -1 to an unsigned int, or other unsigned c++ data type, if I need to get the max value?

Is there any situation where it won't give me the highest value an unsigned data type can contain?

Spectralist
  • 225
  • 2
  • 7

2 Answers2

14

To be on a safe side, use std::numeric_limits<unsigned int>::max(). Casting -1 to unsigned would work on mainstream platforms, but it is not guaranteed by the standard AFAIR.

UPD: I'll correct myself. (unsigned)-1 is required to be UINT_MAX in C, see the answer here

Community
  • 1
  • 1
Andriy
  • 8,486
  • 3
  • 27
  • 51
  • 6
    Casting -1 to an `unsigned` type is guaranteed to work by the standard. The problem is that the intent isn't all that clear. With `std::numeric_limits::max()` it is very clear. Very verbose, also, but very clear. – David Hammen Apr 25 '12 at 07:29
  • 1
    Your update references a c question which in turn references the c standard. The c++ standard says that unsigned ints "shall obey the laws of arithmetic modulo 2^n". The end effect is the same. – David Hammen Apr 25 '12 at 08:19
-1

I think you'll be okay using -1 on any 2's complement machine (which I think is everything nowadays).

Doug Richardson
  • 10,483
  • 6
  • 51
  • 77
  • Ever heard of embedded systems? – RedX Apr 25 '12 at 07:11
  • Not any that support a C++ compiler that use 1s complement. Do you know of any? – Doug Richardson Apr 25 '12 at 07:14
  • I think there was once a question about odd platforms and there were a few examples of 1complements but they were C related. You might be right regarding C++. – RedX Apr 25 '12 at 07:22
  • 3
    From the standard: "Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo 2^n where n is the num- ber of bits in the value representation of that particular size of integer." In other words, unsigned ints are 2s complement even on a 1s complement machine. – David Hammen Apr 25 '12 at 07:27
  • 1
    @DavidHammen I understand what you are trying to say (which is correct), but 2s complement defines a way of representing negative numbers. Since unsigned types can't represent negative numbers at all, they can't be 2s complement. What you probably meant is that conversion from signed to unsigned behaves as if the 2s complement bit pattern were used for the unsigned. – James Kanze Apr 25 '12 at 07:36
  • 2
    @JamesKanze: Yeah, it would have been better to say that how a machine represents negative numbers has no bearing on unsigned integers behave in c++, and that conversion from signed to unsigned proceeds as if the machine used 2s complement to represent negative numbers. – David Hammen Apr 25 '12 at 08:25
  • @DavidHammen: Although it happens that the behavior of the conversion behaves like two's complement, real reason that -1u is UINT_MAX is that UINT_MAX+1 yields zero. – supercat Feb 12 '14 at 00:31
  • 1
    @supercat: The reason UINT_MAX+1 yields zero is precisely because the standard dictates modulo 2^n arithmetic. -1 and UINT_MAX are the same number (modulo 2^n), where n is the number of bits in an unsigned int. – David Hammen Feb 12 '14 at 20:22