6

Consider I have 30-bit RAM to calculate its size: 2^30 = 1073741824 / 10^9 = 1GB

What if I had for example 512 MB RAM is there formula to convert it to n-bit ? this is might be noob question but I'm not good with math :/

4
  • Number of Bytes*8? ;) put attention to the prefix for k M G T or ki... Gi...
    – Hastur
    Jun 26, 2016 at 21:29
  • No I want to convert it to n-bit just like the example in first line
    – user
    Jun 26, 2016 at 21:31
  • 1
    Your first-line calculation is wrong: the result is ~1 Gigabit (1Gb) or ~128 Megabytes (128MB).
    – AFH
    Jun 26, 2016 at 22:16
  • 4
    @AFH I think the OP meant that one gigabyte of RAM can be addressed by a 30-bit pointer, assuming that each RAM location is a byte.
    – Ben N
    Jun 27, 2016 at 0:28

2 Answers 2

17

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.

4
  • 6
    FYI, Google also evaluates log2(536870912) just fine.
    – Bob
    Jun 27, 2016 at 3:56
  • 4
    Feels like log(536870912) / log(2) could be mentioned in case log2() is unavailable. Jun 27, 2016 at 7:24
  • Although it is covered in the linked Wikipedia article, it would improve this answer to add a few words about why you're looking for a logarithm. That way, it can be understood not as this strange mathematical concept, but simply as the inverse operation to exponentiation, which the asker already has a grasp on. Jun 27, 2016 at 12:03
  • you can use powershell [math]::Log(536870912, 2). Or echo "l(536870912)/l(2)" | bc -l or python -c 'import math; print math.log(536870912, 2)'
    – phuclv
    Nov 20, 2018 at 7:55
4

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .