Blake O'Hare .com

Sim City 2000 Integer Overflow

So after Thanksgiving, I felt like I needed to take a break from coding for once and I started digging up some of my ancient computer games. I finally came across Sim City 2000, which sports a 1994 copyright date and proudly boasts Windows 95 compatibility. Remembering the long hours and days lost to this game, I happily shoved the CD into my Windows 7 machine. After playing for about 2 hours, I thought "gee, I ought to save". So I picked Save from the game menu and *POOF*. The Sim City window just disappeared. This was the first hilarity of the evening. Apparently the System-generated Save dialog on Windows 7 is incompatible with this ancient app. So I tried it on my Win XP machine, which, thankfully was still compatible.

Like many games from this era, they were built upon the processor speed, not any sort of controlled frame rate. When I used to play this game, I remember each month taking a few seconds. Now, if I blink, several years go by. If I was making any money at all, suddenly my surplus shoots up in ways the US government could only dream of. So I stopped playing, but left the game on its own for a bit while I went to grab a sandwich at Panera. I came back and I found that my $30,000,000 city budget had found its way to $2,097,000,000. And thus, 2 billion triggered a few Computer Scientist neurons.

Integer Overflows

For the non programmers out there, integers in computers are traditionally stored as 32-digit binary numbers. 32 binary digits means the computer has the ability to store 232 possible values for an integer. Half of them are negative, and half are positive. This gives the highest possible positive value a computer can (traditionally) store as 231. (It's actually one less than that since 0 is thrown in with the positive values) When you add numbers together and go beyond the 231 - 1 threshold, it causes what is called an integer overflow. The value wraps around back to the negative numbers, sometimes causing humorous results.

Punching this into the calculator yields 2,147,483,647. My budget is currently just shy of that by a few game-minutes. Were the MAXIS programmers of the time Y2K-ism-wary and thought of handling integer overflows? Or did they think "eh, it'll never happen". So much to the confusion of onlookers, I sat and stared intently at my budget for the next 2 minutes waiting for something to happen.

And the results...



Click to see full size


And until then, I didn't realize it was actually possible to LOSE at Sim City.