The video for Psy's song "Gangnam Style" is the most-viewed video in the history of YouTube, with more than 2 billion views. And that created some headaches for Google's engineers. Because it stores numbers in binary, YouTube was only designed to record up to 2,147,483,647 video views. Without a software upgrade, the "Gangnam Style" counter would have begun showing the song getting negative 2.1 billion views.

Why is the limit such a goofy number? And what did YouTube have to do to fix the glitch? Read on to find out.

### Why was there a limit to the number of views YouTube could count?

To understand YouTube's Gangnam Style problem, you have to understand how computers count. Human beings count using the decimal number system. As we all learned in grade school, you count 1, 2, 3, get to 9, then "roll over" to 10 and count 11, 12, 13, and so forth.

Computers do things differently from humans in two important ways. First, they use the binary number system, which has two possible values — 0 and 1 — instead of 10. Decimal 1 is binary 1, then decimal 2 is binary 10, decimal 3 is binary 11, decimal 4 is binary 100, and so forth.

Second, when humans write a number like 19, we write however many digits are necessary to represent the number — in this case, the two digits 1 and 9. But computers are storing numbers in electronic circuits with a fixed number of digits. So when they represent a number like 10011 (19 in binary), they pad it out with 0s on the right-hand side. One of the most popular ways to store numbers digitally is with a 32-bit binary number. In this format, decimal 19 would be stored as:

00000000000000000000000000010011

This works great as long as your software is only handling numbers that fit into this format. But weird things happen if computers are asked to handle a number so large that it doesn't fit into the space that was set aside for it. That's what happened in this case: the number of Gangnam Style views was so large that it could no longer fit into the space Google had set aside for it.

### If YouTube was storing 32 binary digits, then it should be able to count up to 2^32 = 4,294,967,296. But the counter broke at 2,147,483,647. How come?

You can use 32 bits to represent any number from 0 to 4,294,967,295. But this has a big disadvantage: you can't represent negative numbers. So computers often set aside the left-most digit to represent a number's sign (0=positive, 1=negative), leaving 31 digits to represent the number itself.

That's what Google did in this case, and 2 to the 31st power is 2,147,483,648. So when Psy got his 2,147,483,648's view, all hell broke loose.

### What happens when a number gets too big to fit in the space reserved for it?

The largest positive integer you can represent in this format is:

01111111111111111111111111111111

If you add one to this amount, you get:

10000000000000000000000000000000

The way computers represent negative numbers is too complicated to explain here (Wikipedia has the gory details), but the bottom line is that this counter "wraps around," going from the highest number that can be represented in this format — 2,147,483,647 — to the lowest: negative 2,147,483,648.

There's a funny xkcd cartoon about this:

So when Psy got his 2,147,483,648th view, YouTube's server would have thought he had minus 2,147,483,648 views.

### How could YouTube fix the problem?

YouTube didn't explain how it fixed the problem in any detail, but one fix is fairly easy: YouTube just had to treat the number as an unsigned integer, treating the left-most digit as part of the number rather than as a minus sign. If you hover over the play count on the Gangnam style video, it first shows the video having -2.1 billion views, then the numbers spin until they show the correct figure.

However, this strategy will only work for another 2 billion views or so. When Psy gets his 4,294,967,296th view, a 32-bit counter will roll over from

11111111111111111111111111111111

to

00000000000000000000000000000000

The system will think he has zero views.

A longer-term fix would be to start using a larger number of bits — most likely 64 — to represent view counts. Then YouTube will be fine until someone gets 2 to the 63rd power views — a number so huge that no video could possibly reach it.