Friday, April 24, 2009

How does -1 become 42.9 million?

I read an interesting blog post over at Zero in a Bit about a slot machine payout. The storey goes as follows: a woman supposedly won $42.9 million dollars from a penny slot machine whose maximal winnings is $9,025. Of course there is litigation, and the woman probably won't get all that money. But, where did a number like 42.9 million come from?

Chris Eng has a pretty good idea. In the slot machine when things go bad, the routine that calculates the winnings probably returns an error code of -1 , and since winning are always positive, they probably use an unsigned int to store the returned value before displaying it to the customer. Ok, so we have an variable that is declared as an unsigned int and we are assigning an int to it, an int that could be negative.

So if the subroutine that calculates the winnings returns an int value of -1, that is 0xFFFFFFFF in two's compliment, then it will be casted to an unsigned int. But, 0xFFFFFFFF is now interpreted as 4,294,967,295 or 4.2 million when we remove the sign. Likely multiplied by 100 at some point (it is a penny arcade), you now get 42.9 million. Oops.

Someone didn't check their error conditions!

4 comments: