The Economist explains

How “Gangnam Style” broke YouTube’s counter

The powers of two permeate computing, but only pop out at odd times

By G.F. | SEATTLE

THE popularity of the “Gangnam Style” video by Psy, a South Korean pop star, is beyond all reckoning. Or at least it was, until a change was made in YouTube's programming. The singer’s video was poised to exceed 2,147,483,647 plays, at which point YouTube would have been unable to count any higher. But the boffins made some tweaks, and now Psy is safe until his rousing anthem passes over nine quintillion views: 9,223,372,036,854,775,808 to be precise. Why couldn’t YouTube count high enough?

The answer involves zooming in to the very building blocks of computing. All numbers in digital computers, whether stored in silicon memory chips or on rapidly rotating magnetically charged hard-disk platters, are represented in binary digits, or bits. In base ten, numbers are represented using ten digits (zero to nine); in binary, or base two, they are represented using two digits (zero and one). For example, in base ten, the digits of a three-digit number correspond to hundreds, tens and units, and the largest number that can be represented is 999; in binary, the digits of a three-digit number correspond to fours, twos and ones, and the largest number that can be represented is 111 (in other words, seven). An eight-digit binary number, which can represent values from 0 to 255, is called a byte; larger numbers are represented using multiple bytes. Two bytes (or 16 bits) can represent numbers from 0 to 65,535; four bytes (or 32 bits) can represent numbers from 0 to 4,294,967,295; and so on. When YouTube was set up, its programmers decided to use a 32-bit number to store the number of views, which seemed a very reasonable choice at the time.

Yet an observant reader will note that the limit on YouTube’s view count was 2,147,483,647, not 4,294,967,295. What explains the discrepancy? The answer is that the 32-bit value is in fact a signed integer: the leftmost digit in a 32-bit binary number ought to be the “2,147,483,648” column (just as the leftmost digit in a ten-digit base-ten number is the “billions” column). But if that column is instead given the negative value -2,147,483,648, it becomes possible to use a 32-bit binary number to store numbers from -2,147,483,648 to 2,147,483,647, rather than 0 to 4,294,967,295. YouTube has no need to count negative views, but some programming languages and databases require the use of signed integers, because it is useful to be able to store either positive or negative values. As a result YouTube ended up with a view-count limit of a little over 2 billion, because that is the largest number that can be represented with a 32-bit signed integer. To fix this problem, its programmers have now switched to using 64-bit signed integers instead. That means the maximum view count is now 9,223,372,036,854,775,807, or a bit over 9 quintillion.

YouTube isn't alone in facing this kind of problem: other systems built years ago also run into numerical-overflow problems. The Unix operating system and all its offshoots and derivatives—which power Android and Apple phones, Macintosh computers and most internet servers—will run out of time on January 19th 2038, at 03:14:07 GMT. By default, Unix counts time in seconds from midnight GMT on January 1, 1970, using a 32-bit signed integer, which means it cannot count more than 2,147,483,647 seconds past that time, thus creating what is known as the “Year 2038 Problem”, akin to the Millennium Bug. Modern versions of the operating system have solved this problem by moving to a 64-bit signed integer to count seconds, but some ancient devices or those running ancient software may fail unpredictably. And perhaps by then, Psy's video count will have crossed the 9 quintillion mark, too.

Dig deeper:
How a really uncool country became the tastemaker of Asia (Aug 2014)
What humanity could achieve if it was not watching Gangnam style (June 2014)
The data behind Gangnam style (Oct 2012)

Discover more

Gaza could face a famine by May. What does that mean?

Some parts of the strip are already experiencing “catastrophic hunger”

What is the Islamic State Khorasan Province?

The group that claimed responsibility for the Crocus City Hall attack is a growing threat to Russia—and the West


Will Texas succeed in enforcing its own immigration law?

The state’s latest challenge to the federal government’s powers, SB4, is in limbo