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 :/

user
  • 79
  • Number of Bytes*8? ;) put attention to the prefix for k M G T or ki... Gi... – Hastur Jun 26 '16 at 21:29
  • No I want to convert it to n-bit just like the example in first line – user Jun 26 '16 at 21:31
  • 1
    Your first-line calculation is wrong: the result is ~1 Gigabit (1Gb) or ~128 Megabytes (128MB). – AFH Jun 26 '16 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 '16 at 00:28

2 Answers2

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.

Ben N
  • 40,965
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
Oriol
  • 1,449
  • 9
  • 25
  • 44