A quadrillion dollars in debt July 15, 2009Posted by Peter Hornby in software, tech.
After an unconscionable period of quiet, the blog bursts into life with a post which will probably only be of interest to a small subset of my vast reader audience.
The BBC reported today that some poor guy in New Hampshire discovered that his bank had debited his account by $23 quadrillion dollars after he bought a packet of cigarettes at a local gas station.
The story concludes:
But no-one has yet explained to Mr Muszynski how such a astonishing error could have been made.
I’m here to help.
The amount quoted in the story is $23,148,855,308,184,500.00
Type eight space characters into a text editor. It’ll look like ” ” on screen (minus the quotes, of course). What has the computer stored in memory? If your text editor uses the ASCII character-encoding scheme, you’ll get 3264 bits which look like this when represented hexadecimally – “2020202020202020”. Each “20” is the ASCII representation of a space character.
Here’s where it gets interesting. Suppose you tell the computer that this memory location doesn’t contain a string, but a decimal value, what do you get? The answer is 2314885530818453536. I’m not sure how the bug manifested itself, but it seems likely that some memory location which was supposed to contain the number of cents in the debit amount was actually overwritten with a text string which was all, or almost all, spaces.
Oh well, geek credentials established. Now back to hummingbirds and choral music.
Updated: Peter points out in comments that eight spaces take 64 bits, not 32. Indeed they do (slaps forehead in annoyance). He also points out that “2020202020201250” converts exactly to the correct decimal value, and wonders whether the pack of cigarettes cost $12.50. Maybe. Thanks for the notes, Peter.