32 Bit Counters: Fail

This post is large­ly a reac­tion to this piece of news: that YouTube/Google nev­er expect­ed the num­ber of views of a YouTube video to exceed the capac­i­ty of a 32 bit counter or 2147483648 views.  This is very Bill Gates 2.0.  I sup­pose the news item is most sur­pris­ing for the who, not the what, but I still find the use of a 32 bit inte­ger by any­one surprising.

32 bit coun­ters rep­re­sent rough­ly four bil­lion val­ues.  In the YouTube case, the 32 bit inte­ger was obvi­ous­ly signed — one bit rep­re­sents the neg­a­tive or pos­i­tive sign and the oth­er 31 bits rep­re­sent the val­ue.  It’s sil­ly to think a video could have neg­a­tive views, but there are cas­es where unsigned 32 bit inte­gers can cause prob­lems (most­ly … when some­where in the mix there’s a signed inte­ger).  But two or four bil­lion isn’t that large.  Cer­tain­ly there are more peo­ple on the inter­net.  Cer­tain­ly one per­son can watch a video more than once.  While it took Gang­nam Style to exceed this num­ber of views, it was inevitable that some­thing would.

In my own pro­gram­ming, I shy away from 32 bit inte­gers unless the class of items is well-defined to be small.  The cost of 8‑byte inte­gers (64 bits) vs. 4‑byte inte­gers (32 bits) is rea­son­ably triv­ial and the cost of fix­ing some­thing lat­er is com­pa­ra­bly large.

You might ask the ques­tion: Is 64 bits enough?  64 bits rep­re­sent 18446744073709551616 val­ues or up to 9223372036854775808 with one bit for the sign.  It’s hard to wrap your mind around these num­bers.  Wikipedi­a’s page on the Order of Mag­ni­tude of Num­bers has this val­ue some­where between the total num­ber of insects on earth and the num­ber of grains of sand on all the beach­es in the world.  So if we’re count­ing things that numer­ous, we need big­ger numbers.

Con­verse­ly, the num­ber of peo­ple in the world has grown from 32 bites to 33 bits (4 to 7 bil­lion) from about 1970 to the present.  There are still many things that fit in 64 bits.  At this point, almost any counter you cre­ate in a soft­ware pro­gram should be 64 bits wide.


This entry was posted in fail and tagged , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.

 characters available