Increment forever and you get -2147483648?

incremental backup advantages and disadvantages
incremental forever backup
full backup
reverse incremental backup
selective backup advantages and disadvantages
incremental forever backup software
incremental vs differential backup easeus
backup types

For a clever and complicated reason that I don't really want to explain (because it involves making a timer in an extremely ugly and hacky way), I wrote some C# code sort of like this:

int i = 0;
while (i >= 0) i++; //Should increment forever
Console.Write(i);

I expected the program to hang forever or crash or something, but, to my surprise, after waiting for about 20 seconds or so, I get this ouput:

-2147483648

Well, programming has taught me many things, but I still cannot grasp why continually incrementing a number causes it to eventually be negative...what's going on here?

In C#, the built-in integers are represented by a sequence of bit values of a predefined length. For the basic int datatype that length is 32 bits. Since 32 bits can only represent 4,294,967,296 different possible values (since that is 2^32), clearly your code will not loop forever with continually increasing values.

Since int can hold both positive and negative numbers, the sign of the number must be encoded somehow. This is done with first bit. If the first bit is 1, then the number is negative.

Here are the int values laid out on a number-line in hexadecimal and decimal:

 Hexadecimal        Decimal
 -----------    -----------
 0x80000000     -2147483648
 0x80000001     -2147483647
 0x80000002     -2147483646
    ...              ...
 0xFFFFFFFE              -2
 0xFFFFFFFF              -1
 0x00000000               0
 0x00000001               1
 0x00000002               2
     ...             ...
 0x7FFFFFFE      2147483646
 0x7FFFFFFF      2147483647

As you can see from this chart, the bits that represent the smallest possible value are what you would get by adding one to the largest possible value, while ignoring the interpretation of the sign bit. When a signed number is added in this way, it is called "integer overflow". Whether or not an integer overflow is allowed or treated as an error is configurable with the checked and unchecked statements in C#. The default is unchecked, which is why no error occured, but you got that crazy small number in your program.

This representation is called 2's Complement.

Types of backup: Full, Incremental, Differential, and Others, Running full backups takes large amounts of time;; You need to have a Similar to all backup types described above, the forever-incremental  >>Would you only ever have incremental forever running with infinite retention?<< Sure, you can do that, but of course this requires you to have theoretically infinite storage. Infinite storage is pretty big. J >>Would you only use standard incremental with cycles (30 days for example) taking a full backup at the beginning of each cycle?<<

The value is overflowing the positive range of 32 bit integer storage going to 0xFFFFFFFF which is -2147483648 in decimal. This means you overflow at 31 bit integers.

It's been pointed out else where that if you use an unsigned int you'll get different behaviour as the 32nd bit isn't being used to store the sign of of the number.

Types of Backup: Full, Incremental, Differential, and more, If I have to restore the backup data from Wednesday, then I have to first Incremental forever backups are similar to the normal incremental  Forever Incremental Use this template to get data from the source virtual machine with the VMware or Hyper-V backup methods. If it is the first job that runs as a part of the Forever Incremental solution, it backs up the full data from the source virtual machine.

What you are experiencing is Integer Overflow.

In computer programming, an integer overflow occurs when an arithmetic operation attempts to create a numeric value that is larger than can be represented within the available storage space. For instance, adding 1 to the largest value that can be represented constitutes an integer overflow. The most common result in these cases is for the least significant representable bits of the result to be stored (the result is said to wrap).

Full vs. incremental vs. differential: Comparing types of backup, What types of backups does your backup application support? As you can see, synthetic full backups and incremental-forever backups can go a  Types of backup. Grandfather, Father, Son (GFS) vs. Progressive (Incremental Forever) Backup Paradigm - Duration: 9:47. storagepipe - Online Backup and Disaster Recovery 23,421 views

int is a signed integer. Once past the max value, it starts from the min value (large negative) and marches towards 0.

Try again with uint and see what is different.

Kenya National Assembly Official Record (Hansard), Mr. Awori: Mr. Deputy Speaker, Sir, I really get very disappointed to find an hon. Member, who has been in You lose one increment forever. If it were going to  It would be recommended that you do your forever incrementals, but also have it do active fulls, periodically. I wouldn't call myself an expert w/ veeam, but the way it was explained to me is that when doing the synthetic full, it uses the chain of incremental to build the full backup.

Try it like this:

int i = 0;
while (i >= 0) 
   checked{ i++; } //Should increment forever
Console.Write(i);

And explain the results

KB1932: Forward Incremental – Animation of Method and Retention, Mr. Awori: Mr. Deputy Speaker, Sir, I really get very disappointed to find an hon. Member, who has been in You lose one increment forever. If it were going to  Increment a variable within a Foreach Loop and use it-SSIS. To increment the SSIS variable you need to do something like How do you get the index of the

C# 2008 For Dummies, Forward incremental-forever backup method is a default method for backup chain If you enable synthetic and/or active full backups, Veeam Backup have changed since the last performed backup (full or incremental) and  Here's a problem I don't have a satisfactory answer to: how do you explain what salary maximums are? That sounds like a dumb question, but bear with me a moment. It seems obvious that for a given job grade we have a salary minimum, a midpoint and a maximum. However when I ask college students studying HR what to do when someone hits the

Forever Forward Incremental Backup, As this example demonstrates, you have to look ahead to see what variables you will Without the increment, year is always 1, and the program loops forever. CJ, thank you for considering StorageCraft products for your incremental forever backup solution, and thank you Ben for the recommendation. If you haven't already, I'd suggest downloading a trial copy of our ShadowProtect software as well as the ImageManager administration tool. ShadowProtect will take your fast and reliable backup (a base full

The forever forward incremental backup method produces a backup chain By continuing to use our website, you agree with our use of cookies in have changed since the last backup job session (full or incremental) and  Does this help you, it would be the same with the -- operator. I know it is not what you really asked but it shows you how the post and pre-incrementer work. so you can add your code to it. int i = 100 ; i = i++ ; //i = 100 the post-increment did not get done (effectively) i = ++i ; //i = 101 the pre-increment did

Comments
  • you aren't implementing a timer, you're implementing a busy loop which is rather naff
  • I'll have to vehemently doubt your clever and complicated reasons. It seems that there is nothing clever about incrementing an integer indefinitely and not even considering that your computer may have limitations; not even considering how a computer actually works before you program it to do a simple task like... counting to infinity (meh, just only infinity, nothing out of the ordinary)
  • +1 just to counter the stupid downvote.
  • possible duplicate of Incrementing an integer value beyond its integer limit - C#
  • Instead, while (true) i++; will increment forever, overflowing every time you "exceed" 2147483647. You can also try while (i != -1).
  • "Since int can hold both positive and negative numbers, the sign of the number must be encoded somehow. This is done with first bit. If the first bit is 1, then the number is negative." Is this true? I believe dotnet uses twos complement to represent signed integers. But I do believe that your reasoning is the right path as to why he gets the small number, is because the twos complement of the most negative number equals the most negative number.
  • @Ernesto - What I said is true, but incomplete. The first bit does represent the sign. I started to describe the rest of the format, but it was getting to be too much, so I just drew a diagram. I put in a link to the full description just as you were commenting.
  • @Jeffrey -- being nit-picky, it is misleading to say that in 2's Complement the most significant bit is not strictly speaking a sign bit: instead it is a binary digit like the other bits, but it is taken to have a negative value where the others are positive. So in a 16-bit integer the LSB (bit 0) has a value of 0 or +1, bit 1 is 0 or +2, ... bit 14 has a value of 0 or +16384 and the MSB (bit 15) has a value of 0 or -32768. (BTW the MSB isn't always the "first bit" -- for example in a little-endian or bit-stream, it doesn't come first. But I think we all knew what you meant!)
  • @Ernesto -- the OP doesn't get a small number, he gets a very large negative number.
  • @Jeffrey, I agree. @AAT, It's sort of a bad habit of talking about negative numbers as if they were smaller than the positive ones. You're absolutely right.
  • To be precise, it didn't overflow a 32-bit value, it overflowed the positive range so it effectively overflowed 31 bits. -2147483648 is 0x80000000; 0xFFFFFFFF is -1.
  • @David it did overflow the positive maximum value for a signed 32-bit integer. An overflow is logically defined by the constraints of the data type, not by having reached the maximally-filled bit pattern of the underlying storage. Otherwise, reaching 0xFFFFFFFF and adding 1 would be the true overflow.
  • And of course, before it can march to 0, the while loop terminates.
  • Yes! A busy timer with BigInt.
  • Ooo could we make it to stackoverflow before we get OOM too - that'd be superfun