Pages

Tuesday, September 6, 2011

Integer Overflow and 00000 Customers Served

Integer data types store values that are mathematical integers. Depending on the programming language and the exact data type, the amount of storage space allocated for an integer can differ. The space directly determines the maximum number of values that can be stored in the variable, and thus the highest value that can be represented. For example, an unsigned 16 bit integer can store 2^16 = 65536 different values-- the numbers 0 to 65535. In some programming languages, assigning a value outside this range will cause the value to "wrap around". Luckily, most modern program languages allow 64 bit integers, providing an upper bound that will match almost every possible use case.

"I like the sign." declared Zed. It was his first visit back to the castle and his pilot store since he had left the coffee business to speculate in coconut sales.

While the new management had resisted making any radical changes to Zed's Coffee shop, they had insisted on a flashy new sign. The sign was at least six feet wide and stood fifteen feet above the ground. It was lit by ten different torches around the base so that it could continue to attract in patrons at night. Some people had claimed that this sign represented the future of coffee advertising.


However, one thing about the sign bothered Zed: the message saying "02053 Served". It reminded him of a problem that he had once encountered with the coffee shop. So he had decided to ask the new manager about it.

"How do you update the sign?" inquired Zed.

"It is pretty simple." answered the manager proudly. "Each digit is a different card. Every morning Rob, over there, climbs up a ladder and changes the digits to reflect the previous night's count. He has to pull down the old cards and put up new ones."

Zed nodded politely. He was, of course, familiar with the principle. Anyone who had been in the coffee industry would know how such a sign worked. He was simply warming up to his real question.

"So, you have five digits -- zero through nine." confirmed Zed. "But what happens when you rollover?"

"Rollover?" asked the manager.

"Yes." replied Zed. "What happens after you serve customer 99,999?"

The manager looked shocked. "We... Well... I... I never thought of that." he answered.

"Ah." responded Zed knowingly. He had considered the same board a few years ago, but his experience with the coffee menu board had taught him to be wary of a fixed number of slots. Specifically, a fixed number of digits limited the maximum number that could be displayed. And, in this case, five digits was absurdly small for a successful coffee chain.

When Zed left, ten minutes later, the coffee shop's manager was still on the phone with the sign company. Zed could hear the manager shout: "I can't just let it overflow. Do you know how bad it would look to have it wrap around to say '00000 served'?"

Zed smiled to himself. While coconut speculation did not have the same level of excitement as coffee shops, sometimes that was a good thing.

-------------------------------

See how Zed's coffee shop got its start with: Arrays, Linked Lists, and Zed's Coffee Shop.

Or learn more about how numbers are represented in binary with: Unhappy Magic Flowers and Binary.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.