23

I have read in many places that unsigned integer overflow is well-defined in C unlike the signed counterpart.

Is underflow the same?

For example:

unsigned int x = -1; // Does x == UINT_MAX?

Thanks.

I can't recall where, but i read somewhere that arithmetic on unsigned integral types is modular, so if that were the case then -1 == UINT_MAX mod (UINT_MAX+1).

erwaman
  • 3,307
  • 3
  • 28
  • 29
snap
  • 711
  • 3
  • 11
  • 25
  • 2
    I believe that the term "underflow" is only really applicable to floating point numbers, where you can't represent some numbers very close to zero. Integers wouldn't have this issue. – WildCrustacean May 03 '10 at 19:09
  • @bde I agree that is a technically accurate statement, but the term is often overloaded for violation of the boundary condition on the bottom end of a number system. – vicatcu May 03 '10 at 19:17

3 Answers3

29

§6.3.1.3 Signed and unsigned integers, paragraph 2:

if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.

So yes, x == UINT_MAX.

oguz ismail
  • 1
  • 16
  • 47
  • 69
Stephen Canon
  • 103,815
  • 19
  • 183
  • 269
4

-1, when expressed as a 2's complement number, amounts to 0xFF...F for how ever many bits your number is. In an unsigned number space that value is the maximum value possible (i.e. all the bits are set). Therefore yes, x == UINT_MAX. The following code emits "1" on a C99 strict compiler:

#include <stdio.h>
#include <stdint.h>
#include <limits.h>

int main(int argc, char **argv){
  uint32_t x = -1;      
  printf("%d", x == UINT_MAX ? 1 : 0);
  return 0;
}
vicatcu
  • 5,407
  • 7
  • 41
  • 65
  • 2
    Are twos-complement numbers required by the standard? – BlueRaja - Danny Pflughoeft May 03 '10 at 19:19
  • 4
    No, 2s complement is not required by the standard, so this solution is not general; see my answer. – Doug Currie May 03 '10 at 19:22
  • 1
    It is not required that the maximum value of `uint32_t` be `UINT_MAX` - `UINT_MAX` can be as small as 65535 and as large as `ULONG_MAX`. If you change that `uint32_t` to `unsigned` it will be correct. – caf May 04 '10 at 01:00
  • 1
    Storing a negative number X in an n-bit unsigned storage space is required to store the smallest non-negative number which is congruent to X mod 2ⁿ. Since the bit pattern for any two's-complement signed number Y will either encode the unsigned value Y or Y+2ⁿ, both of which are congruent mod 2ⁿ, and one of which will be the smallest non-negative number which is congruent mod 2ⁿ, that means two's-complement numbers follow the behavior of unsigned integers--not vice versa. – supercat Jun 24 '15 at 15:34
-4

You are mixing signed and unsigned numbers, which is uncool.

unsigned int x = 0u - 1u; // is OK though
Doug Currie
  • 40,708
  • 1
  • 95
  • 119