A habit I've observed among programmers, and a habit I sometimes subconsciously exhibit myself, is to pick powers of two (or powers of two minus one) when defining a database schema, a data buffer, etc.

Have you made the same observation? If I'm not being blatantly subjective, the follow-up questions are:

Are there still valid reasons to use powers of two [minus one] in modern technologies?

Assuming these habits are mostly vestiges of old technological limitations, I'm just wondering what different flavors of limitations there once were.

Some potential reasons I can think of are data structure optimizations and addressing bits. I'm wondering what else was/is out there...


Are there still valid reasons to use powers of two [minus one] in modern technologies?

The power of two minus one is for 0-based indexing.

32768 items is 0 to 32767. The size is 32768. The last element is 32767. This size vs. last element confuses people all the time.

Physical memory is still managed in 16-byte "paragraphs" -- which are powers of two.

Disks still have blocks that are magical powers of two. The actual number varies by filesystem and OS, but it's a magical power of two.

You may achieve slightly better locality of reference and perhaps save a tiny bit of memory access time. Compilers already do this memory alignment optimization for you. With the multi-level cache on most modern processors, however, you'll have a hard time measuring the impact.

Unless you're writing I/O drivers. In which case, the device and the OS buffers will all involve lots of magical powers of 2 which you must use. When writing I/O drivers, the magical powers of 2 are essential.

For most purposes, however, I prefer to use magical powers of 12. Why not? Retry limits? 12 retries before we raise an exception. Sample data? 12 sample rows from a large file for testing purposes.

  • 1
    12 is nice because it has a lot of factors: 2, 3, 4, 6 Apr 20 '11 at 10:43

Sometiemes it may be just that programers are used to work on base 2.

But there were/are also technological reasons. For instance to make data fit on the caches, or registers, or been able to read the data in one reading operation.

Today when programming with high level languages and so with a big abstraction layer from the machine, most of this optimizations are left to the compiler, because it's not easy to have the deep knowledge of the compiler internals to know how it will optimize your code, but still having this kind of sizes can make things easier.


Actually once a teacher explained to me that the choice of powers of 2 minus 1, are really choices of prime numbers. He told me that this had something to do with data security, something about not beeing hable to divide the data in to equaly sized parts. Never understood the concept.

  • (n^2)-1 isn't always prime though. 16-1 is 15.
    – StuperUser
    Apr 20 '11 at 10:25
  • 1
    I know, the rule described by my programming teacher was to use prime numbers excusively, but It can get really boring calculating prime numbers all the time
    – IvoC
    Apr 20 '11 at 10:54
  • Only Mersenne primes have the property of being 2^p - 1. Maybe that's what your teacher was referring to. Since they're are machine-friendly, some security algorithms may have a preference for Mersenne primes for algorithmic advantages. en.wikipedia.org/wiki/Mersenne_prime
    – Ates Goral
    Apr 20 '11 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.