Calculating bits of RAM

You're looking for a logarithm, a base-two logarithm specifically. Logarithms do the opposite of exponentiation, so if bx = y, then x = logb y. 24 = 16, so log2 16 = 4.

First, you'll need to figure out how many bytes you have. If your number is in kilobytes, multiply by 210. For megabytes, 220, for gigabytes 230, et cetera. As you can see, I'm using the powers-of-1024 definitions of these units rather the powers-of-1000 definitions, so one kilobyte here is 1024 bytes. The unambiguous name for 1024 bytes is kibibyte. Anyway, 512 MB is equal to 512 • 220 = 536870912 bytes.

Now you'll need a scientific calculator. I like Wolfram Alpha, which lets you do base-two logarithms with the log2 function. log2(536870912) produces 29, which makes sense, considering that 512 MB is half of 1 GB, so it takes one less power of two. You can use pretty much any operator imaginable in a Wolfram Alpha expression, so log2(512 * 10^20) works too.

If you get a number with a decimal part, round up. For example, you'd need three bits to address five bytes of RAM, though log2(5) is roughly 2.32.


Additionally to what Ben said, I recommend first doing the logarithm of your number without units

log₂512 = 9

And then take units into consideration: sum 10 for kibibytes, 20 for mebibytes, 30 for gibibytes, ...

9 + 20 = 29

And that's it. No need to calculate huge numbers. That's because logarithms have the following properties:

logₙ(a × b) = logₙ(a) +  logₙ(b)
logₙ(aᵇ) = b × logₙ(a)

Therefore,

log₂(512 × 2²⁰) = log₂(512) + 20

However, if you already know log₂(1 GiB) = 30,

log₂(512 MiB) = log₂(1 GiB / 2) = log₂(1 GiB) - log₂(2) = 30 - 1 = 29