-3
int i=~0;

uint j=(uint)i;

j++;

printf("%u",j);

I am slightly confused as before increment j is "4294967295", but after increment(j++) instead of becoming "4294967296" it is 0...can anyone please explain?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
shweta
  • 1
  • 6
  • 5
    http://en.wikipedia.org/wiki/Integer_overflow The odometer is a very nice explanation image :) – meskobalazs Jan 10 '15 at 07:51
  • What is `uint`? It's probably a typedef defined in some header you haven't shown us. I *hope* it's an alias for `unsigned int`. It's clearer to just refer to `unsigned int` (or `unsigned`) directly. – Keith Thompson Jan 10 '15 at 08:09

2 Answers2

3

The range of 32-bit unsigned int is

0 to 4,294,967,295

So incrementing beyond this value(like +1 to this value) will lead to rolling back/wrap around to 0.

Edits:

§6.2.5 Types ¶9 A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type

Gopi
  • 19,784
  • 4
  • 24
  • 36
  • 1
    What you describe applies to systems which have 32-bit integers. – Jonathan Leffler Jan 10 '15 at 07:53
  • 1
    Note that the term used in the C standard is 'overflow', as in (ISO/IEC 9899:1999 - the old C standard) §6.2.5 **Types** ¶9 _A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type._ – Jonathan Leffler Jan 10 '15 at 08:06
  • @JonathanLeffler Thanks sir for the edits and inputs – Gopi Jan 10 '15 at 08:17
1

It's because it overflows, meaning the data type goes over the maximum value it can represent.

int i = ~0

All bits are set to 1. For an int, this is interpreted as -1.

uint j=(uint)i;

You copy the data and convert it to unsigned int. -1 can't be represented by an unsigned int and will similar to below wrap around so it also has all its bits set to 1.

j++;

When you add by one it overflows. It's easy to see why if you look at the addition in bits. The number is only represented by a certain number of bits, on your machine a int is 32-bit. For a 4-bit number it would look like this:

1111 + 1 = 10000

But the highest order bit has nowhere to be stored, for a unsigned integer this is defined to wrap around like this:

1111 + 1 =  0000
Forss
  • 778
  • 1
  • 4
  • 13
  • 1
    `(uint)i` does not reinterpret the data, it is a value conversion (Gopi's post includes the definition of this). Reinterpret would be `*(uint *)&i` . In 2's complement this turns out to have the same result. – M.M Jan 10 '15 at 08:54